【问题标题】:PreferenceFragment background colorPreferenceFragment 背景颜色
【发布时间】:2013-06-02 22:34:34
【问题描述】:

我正在使用PreferenceFragment(没有ListView),但无法设置背景颜色,而且它似乎是透明的。

如何使用PreferenceFragment 将背景颜色设置为白色?

编辑: 第一张截图:

覆盖 onCreateView 不起作用 -

【问题讨论】:

标签: android background-color preference


【解决方案1】:

这个问题也有回答here

将以下代码添加到您的 PreferenceFragment 将允许您添加背景颜色、图像等。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = super.onCreateView(inflater, container, savedInstanceState);
    view.setBackgroundColor(getResources().getColor(android.R.color.your_color));

    return view;
}

【讨论】:

  • 是否可以仅在首选项控件中更改颜色?让空白区域保持透明。
【解决方案2】:

您还可以在onViewCreated() 方法中更改视图的背景。

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    view.setBackgroundColor(getResources().getColor(R.color.YOUR_COLOR));
}

【讨论】:

  • 这个线程中的实际答案对我不起作用,但这解决了它! :D :D 非常感谢您的解决方案 :D
【解决方案3】:

试试这个。

  @Override
  public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getListView().setBackgroundColor(Color.BLACK);
  }

【讨论】:

    【解决方案4】:

    我遇到了同样的要求(Androidx Preference Screen background for settings fragment)。

    下面的代码对我有用。 (在themes.xml中)

    <resources xmlns:tools="http://schemas.android.com/tools">
        <!-- Base application theme. -->
        <style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
            ...............
           <!-- Add below -->
            <item name="preferenceTheme">@style/preference</item>
        </style>
        
        <style name="preference" parent="Theme.AppCompat">
            <item name="android:background">@color/purple_200</item>
        </style>
    </resources>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-30
      • 1970-01-01
      • 2015-11-29
      相关资源
      最近更新 更多