未找到 Androidx SeekBarPreference xml 属性 setMax

Androidx SeekBarPreference xml attribute setMax not found

我正在尝试实现 androidx SeekBarPreference,根据 docs 我可能可以在我的 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
}

属性是 max 而不是 setMax。你必须做:

app:max="10"

你应该试试这个

在你的XML

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

它会起作用

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

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

您可以这样设置搜索栏:

        <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"