众所周知,在一个继承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的内容。