Androidx SeekBarPreference中未找到setMax属性的xml属性

3
我将尝试实现androidx SeekBarPreference,根据文档,我可以在xml中使用setMax属性,但是当我这样做时会出现以下错误:

在xml中:

<SeekBarPreference
    app:key="preference_key"
    app:title="@string/preference"
    app:showSeekBarValue="true"
    app:setMax="10"/>

错误信息:

root_preferences.xml:53: AAPT: error: attribute setMax (aka 
gls.dev.MyApplication:setMax) not found.

然而,当在代码中设置属性时,它就像魔法一样起作用:

findPreference<SeekBarPreference>("preference_key")?.apply {
    max = 10
    min = 1
    seekBarIncrement = 1
    isAdjustable = true
}
4个回答

6

max属性目前仅存在于android:命名空间中,因此您需要使用:

<SeekBarPreference
    app:key="preference_key"
    app:title="@string/preference"
    app:showSeekBarValue="true"
    android:max="10"/>

1
使用 app:max => 错误:未找到属性 max,使用 android:max => 错误:未绑定前缀。 - VasileM
4
在应用程序命名空间中为什么不存在“max”属性?我认为它应该存在,因为 SeekBarPreference 的所有其他属性都存在。 - Tromse

4

您可以按照以下方式设置您的进度条:

        <SeekBarPreference
        app:title="Choose value:"
        app:defaultValue="2"
        app:min="2"
        app:seekBarIncrement="1"
        android:max="12"
        app:key="key"
        app:adjustable="true"
        app:isPreferenceVisible="true"
        app:showSeekBarValue="true"/>

您还需要将以下内容添加到根标记中:

您还需要将以下内容添加到根标记中:

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

1
"

属性是max而不是setMax。你需要这样做:

"
app:max="10"

对我来说不起作用,错误仍然存在 root_preferences.xml:53: AAPT: error: attribute setMax (aka gls.dev.MyApplication:setMax) not found. - glisu
错误提示是说 setMax,而不是 max。你确定已经将 app:setMax 改为了 app:max 吗? - gpunto
抱歉,我刚才只是复制了之前的错误,现在的错误提示是 root_preferences.xml:54: AAPT: error: attribute max (aka gls.dev.MyApplication:max) not found - glisu
1
尝试使用 android:max 而不是 app:max,就像 @Louis 建议的那样。 - gpunto

0

你应该尝试这个

在你的XML中

<SeekBarPreference
    app:key="preference_key"
    app:title="@string/preference"
    app:showSeekBarValue="true"
    app:max="10"/>

它会工作的


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接