2013-09-26 122 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"); 
       } 
      } 
     }); 

我認爲它有幫助到你.... 謝謝