如何在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的內容。


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