我试图通过活动的onCreate
方法从片段的XML文件中获取android:background属性的颜色集。
我同时使用了getSolidColor()
和getgetDrawingCacheBackgroundColor()
方法,但它们都返回了与包含片段的布局相对应的颜色值。
以下是活动代码部分:
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"/>
包含上述的activity_main xml文件,以及其他4个片段和一个搜索栏:
<?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" />
该方法返回RelativeLayout
的{{1}}而不是activity_main.xml
的值。为什么?
答案 0 :(得分: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()方法并将其存储到类型为casting的ColorDrawable变量中。 然后你可以调用这个对象的getColor()方法来获得所需的颜色。
答案 1 :(得分:1)
将id放在 relative_layout 上,使用 findViewById()获取 relative_layout ,您可以在其中使用view.getBaackground()。