【问题标题】:how to add margin/padding to preference screen如何将边距/填充添加到首选项屏幕
【发布时间】:2014-09-11 09:13:45
【问题描述】:

这是我的代码:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <PreferenceCategory android:title="title" >
        <Preference
            android:layout="@layout/custom_preference_layout"
            android:title="@string/settings_rateUsOnGooglePlay" />

    </PreferenceCategory>
</PreferenceScreen>

不可能在 PreferenceScreen 元素中简单地指定边距/填充。 如何将边距/填充添加到首选项屏幕?

【问题讨论】:

    标签: android android-preferences android-settings


    【解决方案1】:

    11 月2019 年更新: 有一个解决方案,与多年前的第一个答案相反。

    选项 1: 如果您将PreferenceScreen 作为片段膨胀为FrameLayout,您可以像这样将dp 值分配给layout_marginToplayout_marginBottom

            <FrameLayout
            android:id="@+id/fragment_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="64dp" />
    

    选项 2: 否则,您可以访问片段的OnViewCreated 中的RecyclerView 对象(它包含PreferenceScreen 的项目),如下所示:

        // PreferenceFragment class
        @Override
        public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
    
            final RecyclerView rv = getListView(); // This holds the PreferenceScreen's items
            rv.setPadding(0, 0, 0, 56); // (left, top, right, bottom)
        }
    

    【讨论】:

      【解决方案2】:

      不,您不能简单地指定填充/边距。看来您需要指定自定义布局或android:icon="@null",如

      Android: How to adjust Margin/Padding in Preference?

      或者您可以尝试以编程方式设置边距/填充

      Android: How to maximize PreferenceFragment width (or get rid of margin)?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-13
        • 2015-08-14
        • 2022-11-12
        • 2021-05-05
        相关资源
        最近更新 更多