如何在PreferenceActivity中加载普通控件及Preference

众所周知,在一个继承PreferenceActivity的类中,可以通过addPreferencesFromResource(R.xml.preference); 来添加preference。如果要在这个Activity中同时加载普通VIew控件,如TextVIew, Button等,下面是解决方案:

1、新建你要加载的View:

如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"    
    android:orientation="vertical">   
    <TextView android:id="@+id/battery_duration"
        android:layout_width="fill_parent"
	    android:layout_height="50dip"
	    android:gravity="center"
	    android:background="#cccccc"
	    android:textAppearance="@android:attr/textAppearanceMedium"/>
    <ListView android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"/> 	    
</LinearLayout>

要注意的是:ListView这个控件必须有,且id必须@android:id/list,至于为什么,请阅读PreferenceActivity源码。

在onCreate()中,先setContentView(), 再 addPreferencesFromResource()就OK了,其中ListView显示的是preference的内容。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章