Android:更改片段的背景颜色

问题描述:

我尝试更改片段的背景颜色,但发生了一个小问题。Android:更改片段的背景颜色

public class MainActivity extends FragmentActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 
} 

所以,以上所示的代码是我曾经对自己呼吁片段的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> 

以上是main.xml中的布局由主类(MainActivity)调用。

public class FirstFragment extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, 
      Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.main, parent, false); 
    } 
} 

以上与该片的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> 

这种布局上述由类FirstFragment

那么膨胀,为什么没有这实际上改变我的片段的背景颜色?

片段不从View继承,因此没有一组后台方法。

任何简单的解决方法是只片段grab the root view并设置其背景

fragment.getView().setBackgroundColor(Color.WHITE); 
+2

感谢的是解决我的问题,而是getView()我用getRootView():fragment.getRootView()。setBackgroundColor(Color.WHITE); – zarej 2013-04-19 10:05:40

+0

我得到一个NullPointerException当我尝试这PreferenceFragment = s – Solace 2015-07-09 19:42:23

该原因不起作用的原因是因为片段的处理类似于活动。它是一个容器,它是主要活动的子项,用于显示其他项目。

您需要将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> 
+1

这将适用于整个活动,而不是一个特定的片段 – 2016-08-22 23:16:50

我也面临同样的问题,但我的要求是要设置或更改片段的背景绘制的图像。亚当回答说这个方法是正确的,我想展示从片段到活动的连接。

最好的做法是使用(或名称)命名为“连接器”的界面。然后从你的片段:

Connector connector = (Connector) getActivity(); 
connector.setIt(this); 

然后在你的活动将实施“连接器”界面,并有方法。

@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); 
} 

得到这样的片段对象:

Fragment fragment = (Fragment) getFragmentManager().findFragmentById(R.id.fragmentId); 

改变它的背景,如:

fragment.getView().setBackgroundColor(Color.WHITE);