本文主要是介绍PreferenceActivity里自定义标题栏(title bar)出错:requestFeature() must be called before adding content,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一般activity里自定义标题栏时候,我们一般会遵循一个顺序:
1 final boolean isCustom = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 2 setContentView(R.layout.you_layout); 3 if(isCustom){ 4 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_tiltebar); 5 }
就是说,界面的属性(这里指的是window的属性)一般要在展示界面内容(setContentView)之前确定,这个也符合逻辑。然后,今天在我使用preferenceActivity时候,出错了:requestFeature() must be called before adding content。相关代码如下:
super.onCreate(savedInstanceState); final boolean isCustom = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); addPreferencesFromResource(R.xml.preferences); if(isCustom){getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_tiltebar); }
网上查了下,没有结果,于是我觉得是不是super.onCreate(savedInstanceState)里面有文章,进入源码一看,果然:
@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(com.android.internal.R.layout.preference_list_content);mPreferenceManager = onCreatePreferenceManager();getListView().setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); }
可以看出,在super.onCreate(savedInstanceState) 里面就出现了活动界面内容的展示,即调用setcontentview方法。于是把requestFeature()方法放在super.onCreate(savedInstanceState)前面,就ok了
这篇关于PreferenceActivity里自定义标题栏(title bar)出错:requestFeature() must be called before adding content的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!