如何结合首选项和 CardView

How to combine preference and CardView

我正在尝试使用 cardView 和 Preference 创建一个设置菜单,但我不确定如何将这两者结合起来...这是屏幕截图,我想将 ListPreference 和 PreferenceScreen 条目移动到上面的设置 cardView 中。 这是 preference.xml layout/setting_group 是 cardview

<PreferenceScreen
    android:layout="@layout/setting_title_top"
    android:title = "Padding" />
<PreferenceScreen
    android:layout="@layout/setting_group">
</PreferenceScreen>
<ListPreference
    android:id="@+id/list"
    android:layout="@layout/setting_menu"
    android:dialogTitle = "title one"
    android:key = "key_1"
    android:title = "ListPreference"
    android:summary = "Summary"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</ListPreference>

<PreferenceScreen
    android:layout="@layout/setting_menu"
    android:key="Key two"
    android:summary="Summary"
    android:title="PreferenceScreen" />
<PreferenceScreen
    android:layout="@layout/setting_title"
    android:title = "Another Setting" />

PreferenceScreen是一个容器,你在打开和关闭他的时候注意里面。

您可以这样创建首选项:

 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">




   <EditTextPreference
    android:capitalize="words"
    android:defaultValue="@string/default_value" <!--The initial state of the preference -->
    android:key="@string/pref_key" <!-- The key for retrieve the preference -->
    android:singleLine="true"
    android:title="@string/pref_title" />  <!-- The title who is showing above the preference -->


 </PreferenceScreen>

编辑:创建一个 xml 文件以包含您偏好的布局并创建 Activity,示例:

 public class SettingsActivity extends PreferenceActivity
    implements Preference.OnPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.pref_general);
    bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_key))); // Is deprecated, but still a good option to no need change the values manually.


}

private void bindPreferenceSummaryToValue(Preference preference) {
    preference.setOnPreferenceChangeListener(this);

    onPreferenceChange(preference, PreferenceManager
            .getDefaultSharedPreferences(preference.getContext())
            .getString(preference.getKey(), ""));

}


@Override
public boolean onPreferenceChange(Preference preference, Object value) {
    String stringValue = value.toString();
    preference.setSummary(stringValue);



    return true;
}
}