35

Android 默认Switch有文本,然后Switch打开/关闭。我期待Switch开/关,然后发短信。不要使用其他小部件,例如TextView. 如何设置Switch右侧文本。

4

4 回答 4

35

在此处输入图像描述

将其包装在另一个没有文本值的布局中并添加一个文本视图。看我的代码:

    <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>
于 2016-09-12T09:34:15.037 回答
22

Use

android:layoutDirection="rtl"

Then

app:switchPadding="8dp"
于 2018-11-09T10:38:38.807 回答
3

在 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");
                }
            }
        });

我认为它对你有帮助....谢谢

于 2017-06-19T11:33:00.647 回答
0

我能够通过以下组合实现我想要的外观:

  • 宽度(120dp)

  • 内边距 (75dp)

  • 和 switchpadding (-90dp)

在此处输入图像描述

于 2015-06-28T03:05:08.983 回答