【问题标题】:Apply custom theme to PreferenceFragment将自定义主题应用于 PreferenceFragment
【发布时间】:2014-01-30 17:26:02
【问题描述】:

我有左右片段的多窗格视图。在正确的片段上正在启动一个 PreferenceFragment。问题是片段看起来完全扭曲,没有任何样式。有没有办法仅将主题应用于 PreferenceFragment ?

我试过this,但没用

我的代码

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    // create ContextThemeWrapper from the original Activity Context with the custom theme
    final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.AppTheme_PreferenceTheme);

    // clone the inflater using the ContextThemeWrapper
    LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);


    View view = super.onCreateView(localInflater, container, savedInstanceState);

    return view;

}

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState); 

    addPreferencesFromResource(R.xml.app_settings_preference_layout);
}

我认为该解决方案不起作用,因为我已经在 onCreate 中夸大了偏好布局。有没有办法在不使用 addPreferencesFromResource 方法而只使用 LayoutInflater 服务的情况下扩展首选项布局?

【问题讨论】:

    标签: android android-fragments android-preferences


    【解决方案1】:

    我用它来设置 PreferenceFragments 的样式:

    <style name="PreferenceTheme" parent="@style/AppTheme">
        <item name="android:textColor">@color/colorPrimary</item>
        <item name="android:background">@color/cpWhite</item>
        ...
    </style>
    

    然后,在 onCreateView 中:

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ...
        container.getContext().setTheme(R.style.PreferenceTheme);
    }
    

    对我有用。希望对你也有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多