【问题标题】:Android: Change Background Color of FragmentAndroid:更改片段的背景颜色
【发布时间】:2012-07-26 09:20:34
【问题描述】:

我尝试更改片段的背景颜色,但出现了一个小问题。

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类膨胀

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

【问题讨论】:

    标签: android layout background fragment


    【解决方案1】:

    Fragment 不继承自 View,因此没有设置背景方法。

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

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

    【讨论】:

    • 感谢解决了我的问题,但我使用 getRootView() 代替 getView():fragment.getRootView().setBackgroundColor(Color.WHITE);
    • 我在 PreferenceFragment =s 上尝试此操作时收到 NullPointerException
    • @Solace - 我认为这意味着它还不是可视化树的一部分。尝试将其设置为稍后调用的覆盖。
    【解决方案2】:

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

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

    【讨论】:

    • 这将适用于整个活动,而不是特定的片段
    【解决方案3】:

    像这样获取片段对象:

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

    改变它的背景:

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

    【讨论】:

      【解决方案4】:

      在 AndroidX 中,您可以使用 FragmentContainerView 而不是 Fragment 来设置背景:

      <androidx.fragment.app.FragmentContainerView
          ...
          android:background="#FFFFFF"/>
      

      【讨论】:

        【解决方案5】:

        我遇到了同样的问题,但我的要求是设置或更改片段的背景可绘制图像。正如亚当回答的那样,这种方法是正确的,我想展示从片段到活动的联系。

        最佳做法是使用名为“连接器”(或任何名称)的接口。然后从您的片段中:

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

        【讨论】:

          猜你喜欢
          • 2013-04-27
          • 1970-01-01
          • 2021-08-31
          • 2019-01-16
          • 2014-11-08
          • 2020-10-07
          • 1970-01-01
          • 1970-01-01
          • 2013-08-08
          相关资源
          最近更新 更多