我想将PreferenceCategory
显示为CardView
。
在Lollipop官方偏好设置屏幕中,他们不直接使用CardView
,但不同的偏好类别之间也存在差距。我怎么能这样做?
这是我的首选项代码:
PrefsFragment(PreferenceFragment)
/**
* Created by xisberto on 08/11/14.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class PrefsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String category = getArguments().getString("category");
if (category != null) {
if (category.equals(getString(R.string.category_general))) {
addPreferencesFromResource(R.xml.prefs_general);
} else if (category.equals(getString(R.string.category_advanced))) {
addPreferencesFromResource(R.xml.prefs_erscheinungsbild);
}
}
}
}
偏好设置(PreferenceActivity)
public class Preferences extends PreferenceActivity {
private Toolbar mToolBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prepareLayout();
buildLegacyPreferences();
}
private void prepareLayout() {
ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
View content = root.getChildAt(0);
LinearLayout toolbarContainer = (LinearLayout) View.inflate(this, R.layout.activity_prefs, null);
root.removeAllViews();
toolbarContainer.addView(content);
root.addView(toolbarContainer);
mToolBar = (Toolbar) toolbarContainer.findViewById(R.id.toolbar);
mToolBar.setTitle(getTitle());
mToolBar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
private void buildLegacyPreferences() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
String action = getIntent().getAction();
if (action == null) {
addPreferencesFromResource(R.xml.headers_legacy);
} else if (action.equals(getString(R.string.category_general))) {
mToolBar.setTitle(getString(R.string.header_general));
addPreferencesFromResource(R.xml.prefs_general);
} else if (action.equals(getString(R.string.category_advanced))) {
mToolBar.setTitle(getString(R.string.header_general));
addPreferencesFromResource(R.xml.prefs_erscheinungsbild);
}
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onBuildHeaders(List<Header> target) {
super.onBuildHeaders(target);
loadHeadersFromResource(R.xml.headers, target);
}
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
protected boolean isValidFragment(String fragmentName) {
return fragmentName.equals(PrefsFragment.class.getName());
}
public static int getToastDuration(Context context) {
return (PreferenceManager.getDefaultSharedPreferences(context).getString(PreferenceNames.TOAST_DURATION, "0").equals("0")) ? 0 : 1;
}
}
答案 0 :(得分:0)
您必须使用此CardView制作另一个布局/ xml文件(f.e.preference_category_layout),然后
<android.support.v7.preference.PreferenceCategory
android:layout="@layout/preference_category_layout">