我试图通过 Activity 的 onCreate
方法从 fragment 的 XML 文件中获取 android:background 属性上设置的颜色。
我使用了 getSolidColor()
和 getgetDrawingCacheBackgroundColor()
方法,但它们都返回与包含 fragment 的布局相对应的颜色值。
这是 Activity 代码部分:
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()
fragment 的 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 个 fragment 和一个搜索栏:
<?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
的 RelativeLayout
返回值,而不是从 fragment_one.xml
返回值。为什么?
最佳答案
找到了解决此问题的方法:
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();
必须调用 fragment 的 getBackground() 方法并将其存储到具有类型转换的 ColorDrawable 变量中。 然后调用该对象的getColor()方法即可获取所需的颜色。
关于java - 安卓 : Get fragment's background color,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37509146/