Android:如何在首选项屏幕中删除边距/填充 [英] Android: How to remove margin/padding in Preference Screen

查看:342
本文介绍了Android:如何在首选项屏幕中删除边距/填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设计首选项屏幕时,我面临非常奇怪的问题.尽管我没有在布局上留出任何余地,但仍留有一些空间.

I am facing very weird problem in designing preference screen. Though I am not giving any margin in layout,it is leaving some space in left.

如下图所示:

XML:

   <PreferenceScreen android:title="demo" >
       <CheckBoxPreference
           android:defaultValue="false"
            android:key="prefSync"`
            android:title="Auto Sync" />
    </PreferenceScreen>

在屏幕上添加复选框首选项时我做错了吗?

Am I doing something wrong in adding check-box preference in screen?

推荐答案

为androidx更新.

Updating this for androidx.

经过大量实验,我通过将这个添加到具有多余缩进的每个首选项中来解决此问题:

After a lot of experimentation, I resolved this issue by adding this to each preference that had the excess indentation:

app:iconSpaceReserved="false"

当然,您还需要将其添加到xml顶部的PreferenceScreen声明本身中:

Of course, you'll also need to add this to the PreferenceScreen declaration itself at the top of your xml:

xmlns:app="http://schemas.android.com/apk/res-auto"

关注自定义首选项

我注意到,在自定义首选项的情况下,尤其是在较旧的设备上,此解决方案并不总是有效.例如,仍然可以缩进这样的首选项:

I noticed that in the case of custom preferences, particularly on older devices, this solution wasn't always working. For example, a preference like this could still be indented:

com.example.preference.SomeCustomPreference
    android:id="@+id/some_custom_preference"
    android:name="Custom Preference"
    android:key="@string/custom_pref_key"
    android:summary="@string/custom_pref_summary"
    android:title="@string/preference_custom_title"
    app:iconSpaceReserved="false"

该问题追溯到自定义首选项类中第三个构造函数参数的用法.如果您传递第三个参数,则列表项将缩进.如果您省略它,则列表将正确对齐:

The problem traces to the usage of the third constructor parameter in the custom preference class. If you pass that third parameter, the list item will be indented. If you omit it, the list will be aligned correctly:

class SomeCustomPreference
@JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = android.R.attr.someCustomPreferenceStyle
) : DialogPreference(context, attrs, defStyle) {

    override fun getDialogLayoutResource(): Int {
        return R.layout.my_layout
    }
}

相反,请使用:

class SomeCustomPreference
@JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null
) : DialogPreference(context, attrs) {

    override fun getDialogLayoutResource(): Int {
        return R.layout.my_layout
    }
}

在此非常古老的帖子中将其贷记给@CommonsWare.

Credit for this goes to @CommonsWare in this very old post.

这篇关于Android:如何在首选项屏幕中删除边距/填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆