添加 Preference 文件:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.system_cloud_sync_settings); }
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_cloud_sync_settings, null); }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="10dip" android:paddingLeft="30dip" android:orientation="vertical" > <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/cloud_sync" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/tv_summary" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/cloud_sync_title_summary" /> <View android:layout_width="fill_parent" android:layout_height="2dp" android:background="?android:attr/listDivider" /> <!-- 这个根ListView必不可少,否则 Preference 无法加载,报错 --> <include layout="@layout/settings_root_layout" /> </LinearLayout>
<ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:fadingEdgeLength="0dip" android:scrollbarStyle="outsideOverlay" android:scrollbars="none" />