如何在Android中将开关文本改为右侧显示?

40

默认情况下,Android的Switch小部件有文本,然后是Switch的开/关状态。我希望能够得到先显示Switch的开/关状态,再显示文本。不要使用其他小部件,例如TextView。如何在Switch的右侧设置文本。


1
在你的xml中使用像android:textOn和android:textOff这样的xml属性。 - Jitesh Dalsaniya
1
@JiteshDalsaniya 默认情况下,开关具有开关文本左侧和开关右侧。我期望开关左侧和开关文本右侧。现在你明白了吗?我期望的是什么。 - Murali Ganesan
2
可能是[如何将文本位置设置为Switch右侧(类似于CheckBox)]的重复问题(https://stackoverflow.com/questions/45983522/how-to-set-text-position-to-the-right-of-a-switch-like-checkbox)。 - Tin Man
4个回答

39

输入图像描述

将其包装在另一个没有文本值的布局中,并添加一个TextView。请参阅我的代码:

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:layout_height="wrap_content">
            <Switch
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/reminder" />
            <TextView
                android:layout_width="wrap_content"
                android:text="Meeting Reminders"
                android:layout_height="wrap_content" />
    </LinearLayout>

5
功能很好,但如何在单击“会议提醒”时实现切换“开关”控件,而不需要在活动中编写代码逻辑?例如,使用“for =“@id / reminder””。 - mihkov
@keithics 不太相关的问题: “Meeting Reminders” 中使用的字体是哪种? - Detained Developer
Roboto,我想是一个旧项目,抱歉,我记不清了。 - keithics
@mihkov 不幸的是,它需要添加到代码中,但只需一行: linearLayout.setOnClickListener { switch.toggle() } - Wojtek
这与TalkBack不兼容。 - arekolek

28

使用

android:layoutDirection="rtl"

然后

app:switchPadding="8dp"

18
这是一个肮脏的技巧,我个人认为不可取,因为它也会逆转按钮的行为,对用户体验会产生负面影响。 - siyb
1
我同意@siyb的观点,“rtl”设计是为了从右到左书写和阅读的语言,如阿拉伯语。 - Amir Dora.
android:layoutDirection="rtl" 对于 Android 11+ 不起作用。 - AndyBoy

2

将此代码添加到Xml文件中:

            <Switch
                android:id="@+id/simpleSwitch"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"

                android:layout_marginEnd="15dp"
                android:layout_marginRight="15dp"
                android:layout_marginTop="5dp"
                android:checked="false"
                android:textColor="@color/black"
                android:textOff="@string/no"
                android:textOn="@string/yes"
                android:theme="@style/SCBSwitch" />

            <TextView
                android:id="@+id/switchBtn_txtView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="5dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="10dp"
                android:layout_toLeftOf="@+id/simpleSwitch"
                android:gravity="center_horizontal" />

在活动或片段中:

Switch simpleSwitchBtn = (Switch) findViewById(R.id.simpleSwitch);
        final TextView switchBtn_txtView = (TextView) findViewById(R.id.switchBtn_txtView);


        simpleSwitchBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    switchBtn_txtView.setText("Yes");
                }
                else {
                    switchBtn_txtView.setText("No");
                }
            }
        });

我认为这对你很有帮助...谢谢


0

我通过以下方式成功实现了我想要的外观:

  • 宽度(120dp)

  • 填充(75dp)

  • 和开关填充(-90dp)

enter image description here


1
负边距让我感到不安。但可能这是在没有额外TextView的情况下完成它的唯一方法...这会在某些情况下引起问题吗? - findusl
这是你应该使用的答案:https://dev59.com/AGMk5IYBdhLWcg3wtgQR#53224113 - Mbuodile Obiosio
我喜欢这个答案。 - Kibi
只要开关中的文本更改,这将会出现错误,而这可能是由于多种原因引起的(应用程序使用不同的语言,用户更改设备上的字体缩放等)。 - Simon Raes

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