Jee*_*van 7 android android-preferences android-fragments
我有左窗口和右边片段的多窗格视图.在右侧片段上启动PreferenceFragment.问题是片段看起来完全失真没有任何风格.有没有办法仅将主题应用于PreferenceFragment?
我尝试了这个,但它没有用
我的代码
@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);
}
Run Code Online (Sandbox Code Playgroud)
我认为解决方案不起作用,因为我已经在onCreate中夸大了首选项布局.有没有办法在不使用addPreferencesFromResource方法而只使用LayoutInflater服务的情况下对首选项布局进行膨胀?
小智 2
我用它来设置 PreferenceFragments 的样式:
<style name="PreferenceTheme" parent="@style/AppTheme">
<item name="android:textColor">@color/colorPrimary</item>
<item name="android:background">@color/cpWhite</item>
...
</style>
Run Code Online (Sandbox Code Playgroud)
然后,在 onCreateView 中:
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
...
container.getContext().setTheme(R.style.PreferenceTheme);
}
Run Code Online (Sandbox Code Playgroud)
对我有用。我希望它也对你有帮助。
| 归档时间: |
|
| 查看次数: |
2381 次 |
| 最近记录: |