簡體   English   中英

如何在android中將Switch文本更改為右側?

[英]How to change Switch text to right side in android?

Android默認Switch有文本,然后Switch /關閉。 我期待Switch的開/關,然后文本。 不要使用像TextView這樣的其他小部件。 如何設置Switch右側文本。

在此輸入圖像描述

將其包裝在另一個沒有文本值的布局中,並添加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>

使用

android:layoutDirection="rtl"

然后

app:switchPadding="8dp"

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

我覺得它對你很有幫助....謝謝

對於基於rtl的控件,請使用CheckBox而不是Switch

我能夠通過以下組合實現我想要的外觀:

  • 寬度(120dp)

  • 填充(75dp)

  • 和switchpadding(-90dp)

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM