Android:更改片段的背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11692162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: Change Background Color of Fragment
提问by Brandon Ling
I tried changing the background color of a fragment, but a small problem occurred.
我尝试更改片段的背景颜色,但发生了一个小问题。
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
So, shown aboveis the code I had for my main class that calls the XML file for the fragment.
所以,上面显示的是我的主类的代码,它调用片段的 XML 文件。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
android:id="@+id/fragment1"
android:name="com.northreal.practice.FirstFragment"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#CBA" />
</LinearLayout>
Aboveis the main.xml layout that is called by the main class (MainActivity).
以上是主类(MainActivity)调用的main.xml布局。
public class FirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.main, parent, false);
}
}
Abovethe XML file with the fragment calls this class.
上面带有片段的 XML 文件调用这个类。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BLAHHHH"
android:layout_gravity="center_vertical" />
</LinearLayout>
This layout aboveis inflated by the class FirstFragment
上面的这个布局是由类 FirstFragment 膨胀的
So, why doesn't this actually change the color of the background of my fragment?
那么,为什么这实际上并没有改变我的片段背景的颜色?
回答by Adam
Fragments don't inherit from View and thus don't have a set background method.
片段不从 View 继承,因此没有设置背景方法。
Any easy fix is to just grab the root viewof fragment and set its background
fragment.getView().setBackgroundColor(Color.WHITE);
回答by Brandon Ling
The reason why that doesn't work is because fragment is treated similar to an activity. It is a container that is the child of the main activity, and is used to display other items.
这不起作用的原因是因为片段被视为类似于活动。它是一个容器,是主要活动的子项,用于显示其他项目。
You need to put the android:background="#CBA" in the actual layout that holds the TextView and NOT the fragment itself. Like so:
您需要将 android:background="#CBA" 放在包含 TextView 而不是片段本身的实际布局中。像这样:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CBA"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="BLAHHHH" />
</LinearLayout>
回答by eldhoittangeorge
Get the fragment object like:
获取片段对象,如:
Fragment fragment = (Fragment) getFragmentManager().findFragmentById(R.id.fragmentId);
Change it's background like:
改变它的背景,如:
fragment.getView().setBackgroundColor(Color.WHITE);
回答by nickhoffmann7
In AndroidX you can use a FragmentContainerView instead of a Fragment to set a background:
在 AndroidX 中,您可以使用 FragmentContainerView 而不是 Fragment 来设置背景:
<androidx.fragment.app.FragmentContainerView
...
android:background="#FFFFFF"/>
回答by Asad
I have faced the same problem but my requirement was to set or change background drawable image of fragment. As Adam answered the approach is right and I would like to show the connection from fragment to activity.
我遇到了同样的问题,但我的要求是设置或更改片段的背景可绘制图像。正如亚当回答的那样,方法是正确的,我想展示从片段到活动的联系。
The best practice is to use an interface named 'Connector'(or any name).Then from your fragment:
最佳做法是使用名为“Connector”(或任何名称)的接口。然后从您的片段中:
Connector connector = (Connector) getActivity();
connector.setIt(this);
Then in your activity which will implement 'Connector' interface and have the method.
然后在您的活动中,它将实现“连接器”接口并具有该方法。
@Override
public void setIt(Fragment fragment){
FirstFragment firstFrag = (FirstFragment) getSupportFragmentManager().findFragmentByTag("first");
firstFrag.getView().setBackgroundDrawable(getResources().getDrawable(R.drawable.app_background));
//or for color set
firstFrag.getView().setBackgroundColor(Color.WHITE);
}