PreferenceActivity中添加普通view组件

在一个集成PreferenceActivity的类中,可以通过
addPreferencesFromResource(R.xml.preference);
加载preference控件,现在想在PreferenceActivity添加一个Button或是一个checkBox如何办到呢,

1、新建一个Layout,文件名为set_preference_main.xml,文件内容如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical">
    <Button 
    	android:layout_width="fill_parent" 
    	android:layout_height="wrap_content" 
    	android:text="Button"></Button>
    	
    <ListView android:id="@android:id/list"
    	android:layout_width="fill_parent" 
    	android:layout_height="wrap_content"></ListView>
</LinearLayout>

其中Button为自己需要添加的view,ListView会被R.xml.preference的preferences替换。
注意其中ListView的android:id="@android:id/list"必须,且不可改变。

2、在Activity的onCreate中添加
public void onCreate(Bundle savedInstanceState) {  
       super.onCreate(savedInstanceState);  
         
       addPreferencesFromResource(R.xml.preference);  
       setContentView(R.layout.set_preference_main);  
}

注意其中setContentView(R.layout.set_preference_main);表示加载set_preference_main.xml内容到content中

猜你喜欢

转载自iaiai.iteye.com/blog/1880507