2013-09-26 94 views
6

Android默认Switch有文字,然后Switch开/关。我正在打开/关闭Switch,然后是文本。不要使用其他小部件,如TextView。我如何设置Switch右侧文字。如何在Android中将切换文本切换到右侧?

+0

在xml中使用像android:textOn和android:textOff这样的xml属性。 –

+1

@JiteshDalsaniya默认开关有开关文本左侧和开关有右侧。我期待切换左侧并切换文本右侧。你现在可以清理吗?期望 –

回答

-2

我能够实现我想用的组合看:

  • 宽度(120dp)

  • 填充(75dp)

  • 和switchpadding(-90dp)

enter image description here

8

enter image description here

把它包在一个没有文本值另一个布局,并添加一个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> 
+1

很好用,但是如何在'Meeting Reminders'上点击来切换'Switch'控件时如何实现,没有活动中的代码逻辑?例如'for =“@ id/reminder”' – mihkov

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

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