【问题标题】:How to get Android system preferences look?如何获取 Android 系统偏好设置?
【发布时间】:2016-02-04 16:31:17
【问题描述】:

棒棒糖上的 android 系统偏好设置屏幕似乎正在使用类似于“卡片布局”的布局。

我正在尝试为我的应用程序获得相同的外观,但无法看到他们如何使用默认的 PreferenceFragment 实现它。谁能给我解释一下,我怎样才能达到同样的效果而不必写我自己的偏好?


请注意,我已经看过并阅读了这些问题,但它们没有提供合适的答案:


如果感兴趣,我当前的偏好 xml 如下所示:

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

    <PreferenceCategory android:title="@string/preferences_title_general">
        <Preference
            android:key="Prefs.Delete.Row.Token"
            android:summary="@string/preferences_delete_row_token_summary"
            android:title="@string/preferences_delete_row_token_title"/>
        <Preference
            android:key="Prefs.Delete.Cell.Token"
            android:summary="@string/preferences_delete_cell_token_summary"
            android:title="@string/preferences_delete_cell_token_title"/>
        <Preference
            android:key="Prefs.Null.Token"
            android:summary="@string/preferences_null_token_summary"
            android:title="@string/preferences_null_token_title"/>
    </PreferenceCategory>
    <PreferenceCategory android:title="@string/preferences_title_appearance">
        <SwitchPreference
            android:dialogTitle="@string/preferences_theme_title"
            android:key="Prefs.Appearance.Theme"
            android:summaryOff="@string/preferences_theme_summary_off"
            android:summaryOn="@string/preferences_theme_summary_on"
            android:switchTextOff="@string/general_no"
            android:switchTextOn="@string/general_yes"
            android:title="@string/preferences_theme_title"/>
    </PreferenceCategory>
    <PreferenceCategory android:title="@string/preferences_title_misc">
        <SwitchPreference
            android:dialogTitle="@string/preferences_read_back_title"
            android:key="Prefs.Voice.Feedback"
            android:switchTextOff="@string/general_no"
            android:switchTextOn="@string/general_yes"
            android:title="@string/preferences_read_back_title"/>
        <Preference
            android:key="Prefs.Export.Barcode.Properties"
            android:title="@string/preferences_export_title"
            android:summary="@string/preferences_export_summary"/>
        <Preference
            android:key="Prefs.Show.Eula"
            android:title="@string/preferences_eula_title"
            android:summary="@string/preferences_eula_summary"/>
    </PreferenceCategory>

</PreferenceScreen>

我扩展 PreferenceFragment 并通过加载 xml

addPreferencesFromResource(R.xml.prefs);

【问题讨论】:

  • 手头没有任何 api >20 设备,您是否尝试使用设备监视器查看它 -> 转储视图层次结构?
  • @DavidMedenjak 这样做给了我布局的层次结构。现在我该怎么办?正如我所说,我不想在正常布局中重现布局结构,而是想让我现有的 PreferenceFragment 看起来一样。

标签: android layout material-design android-preferences


【解决方案1】:

我已经通过在 PreferenceCategory 项目之间添加一个虚拟 Preference 来“解决”这个问题,该项目使用的布局模仿问题中屏幕截图中的外观:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:custom="http://schemas.android.com/apk/res-auto">
    <PreferenceCategory></PreferenceCategory>

    <Preference layout="@layout/preference_divider" />

    <PreferenceCategory></PreferenceCategory>
</PreferenceScreen>

布局看起来像这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:custom="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <View     android:layout_width="match_parent"
              android:layout_height="5dp"
              android:background="@drawable/shadow_bottom" />

    <View     android:layout_width="match_parent"
              android:layout_height="5dp"
              android:background="@drawable/shadow_top" />

</LinearLayout>

【讨论】:

  • 你是如何定义 shadow_top 和 shadow_bottom 的?
  • @JavierDelgado 它们只是图像 (9-patch)。
  • 你有机会分享这些吗?
【解决方案2】:

对我来说,通过自定义布局设置背景是行不通的。高度也不适用。

这对我有用:

public class PreferenceDivider  extends Preference {
    public PreferenceDivider(Context context) {
        super(context);
    }
    public PreferenceDivider(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    protected View onCreateView(ViewGroup parent) {
        Resources res=getContext().getResources();
        View v=super.onCreateView(parent);
        v.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, res.getDimensionPixelSize(R.dimen.divider_height)));
        v.setBackground(res.getDrawable(R.drawable.preference_category_divider, null));
        return v;
    }
}

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:custom="http://schemas.android.com/apk/res-auto">
    <PreferenceCategory/>>

    <your.package.path.PreferenceDivider/>

    <PreferenceCategory/>>

</PreferenceScreen>

9 个补丁图像在这里:https://drive.google.com/file/d/1M5uXXnFc0HHx2BOHQqLtvAXFet4ICDj0/view?usp=sharing

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 2014-04-21
    • 2018-02-19
    • 2015-09-19
    • 2016-05-12
    • 2016-09-15
    相关资源
    最近更新 更多