衆所周知,在一個繼承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的內容。