2016-05-29 130 views
0

我想通过活动的onCreate方法从片段的XML文件中获取android:background属性上的颜色集。 我用两个getSolidColor()getgetDrawingCacheBackgroundColor()方法,但它们两者的返回对应于含有该片段的布局的颜色的值..Android:获取片段的背景颜色

这里是活动代码部分:

private SeekBar seekBar; 
private View fragmentOne; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    fragmentOne = findViewById(R.id.fragment1); 
    seekBar = (SeekBar) findViewById(R.id.seekBar1); 
    fragmentOne.getDrawingCacheBackgroundColor() 

片段的XML文件,

<RelativeLayout 
android:layout_height="match_parent" 
android:layout_width="match_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:background="@color/colorAccent" 
android:padding="5dp"/> 

而且含有上述,也4其它片段和搜索条的activity_main xml文件:

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/black"> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 



     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="2" 
      android:orientation="vertical" 
      android:padding="5dp" 
      android:paddingBottom="@dimen/activity_vertical_margin" 
      android:paddingLeft="@dimen/activity_horizontal_margin" 
      android:paddingRight="@dimen/activity_horizontal_margin" 
      android:paddingTop="@dimen/activity_vertical_margin" 
      android:layout_marginBottom="20dp" 
      android:layout_marginTop="20dp" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior" 
      tools:context="com.maryland.modernartui_nikos.MainActivity"> 





      <fragment 
       android:id="@+id/fragment1" 
       android:name="com.maryland.modernartui_nikos.FragmentOne" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="2" 
       tools:layout="@layout/fragment_fragment_one" /> 

      <fragment 

       android:id="@+id/fragment2" 
       android:name="com.maryland.modernartui_nikos.FragmentTwo" 
       android:layout_width="match_parent" 
       android:layout_height="117dp" 
       tools:layout="@layout/fragment_fragment_two" /> 


     </LinearLayout> 


     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="2" 
      android:orientation="vertical" 
      android:padding="2dp" 
      android:paddingBottom="@dimen/activity_vertical_margin" 
      android:paddingLeft="@dimen/activity_horizontal_margin" 
      android:paddingRight="@dimen/activity_horizontal_margin" 
      android:paddingTop="@dimen/activity_vertical_margin" 
      android:showDividers="beginning|middle|end" 
      android:layout_marginBottom="20dp" 
      android:layout_marginTop="20dp" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior" 
      tools:context="com.maryland.modernartui_nikos.MainActivity"> 


      <fragment 
       android:id="@+id/fragment3" 
       android:name="com.maryland.modernartui_nikos.FragmentThree" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="2" 
       tools:layout="@layout/fragment_fragment_three" /> 

      <fragment 

       android:id="@+id/fragment4" 
        android:name="com.maryland.modernartui_nikos.FragmentFour" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="4" 
       tools:layout="@layout/fragment_fragment_four" /> 


      <fragment 

       android:id="@+id/fragment5" 
       android:name="com.maryland.modernartui_nikos.FragmentFive" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="4" 
       tools:layout="@layout/fragment_fragment_five" /> 


     </LinearLayout> 



    </LinearLayout> 

    <SeekBar 
     android:id="@+id/seekBar1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:max="10" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" /> 

该方法返回的值为的activity_main.xml而不是fragment_one.xml。为什么?在relative_layout

回答

2

找到了解决此问题:

 ColorDrawable colorDrawableOne = (ColorDrawable) fragmentOne.getBackground(); 
    ColorDrawable colorDrawableTwo = (ColorDrawable) fragmentTwo.getBackground(); 
    ColorDrawable colorDrawableFour = (ColorDrawable) fragmentFour.getBackground(); 
    ColorDrawable colorDrawableFive = (ColorDrawable) fragmentFive.getBackground(); 

    final int colorOne = colorDrawableOne.getColor(); 
    final int colorTwo = colorDrawableTwo.getColor(); 
    final int colorFour = colorDrawableFour.getColor(); 
    final int colorFive= colorDrawableFive.getColor(); 

片段的的getBackground()方法,必须调用并存储到与型铸造ColorDrawable变量。 然后,您可以调用此对象的getColor()方法以获取所需的颜色。

1

放ID,使用findViewById()得到relative_layout,在那里你可以使用view.getBaackground()。