【问题标题】:Android Preference how to select one of multiple items next to each otherAndroid Preference如何选择彼此相邻的多个项目之一
【发布时间】:2020-06-06 14:32:13
【问题描述】:

Preference Example of multiple selections next to each other

我正在尝试将首选项添加到我的 PreferenceScreen 中,以允许用户从相邻的一行中的多个条件中选择一个条件(如上图)。

android 有没有一种预先构建的方式来做到这一点,或者我必须创建一个自定义首选项布局来实现这一点。如果是的话,最好的方法是什么。

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:title="Preferences">
<Preference
    android:key="title"
    android:title="title"
    android:layout="@layout/preference_title"
    />
<SwitchPreference
    android:key="dark_mode"
    android:title="DarkMode"
    android:defaultValue="false"/>

//Instead of Switch Preference for Dark Mode 

</PreferenceScreen>

【问题讨论】:

    标签: android android-preferences preference switchpreference


    【解决方案1】:

    没有这样的预置偏好布局。您需要使用水平 RadioGroup 创建自定义首选项。

    two_inline_button_layout.xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingStart="?android:listPreferredItemPaddingStart"
        android:paddingEnd="?android:listPreferredItemPaddingEnd">
    
        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
    
            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="start" />
        </FrameLayout>
    
        <RadioGroup
            android:id="@+id/two_inline_button_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <RadioButton
                android:id="@+id/left"
                android:layout_weight="1.0"
                android:button="@android:color/transparent" />
    
            <RadioButton
                android:id="@+id/right"
                android:layout_weight="1.0"
                android:button="@android:color/transparent" />
        </RadioGroup>
    </LinearLayout>
    

    你的偏好 xml

    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:custom="http://schemas.android.com/apk/res-auto"
        app:title="Preferences">
    
        <InlineButtonPreference
            android:key="dark_mode"
            android:layout="@layout/two_inline_button_layout"
            custom:titleText="DarkMode"
            custom:startText="ON"
            custom:endText="OFF" />
    </PreferenceScreen>
    

    TwoInlineButtonPreference.kt

    class TwoInlineButtonPreference : Preference {
        
        ....
    
        override fun onBindView(view: View) {
            super.onBindView(view)
            val radioGroup = view.findViewById(R.id.two_inline_button_layout) as RadioGroup
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      相关资源
      最近更新 更多