2013-09-26 60 views
6

Android mặc định Switch có văn bản rồi bật/tắt Switch. Tôi đang mong đợi Switch bật/tắt rồi chạm vào văn bản. Không sử dụng các tiện ích con khác như TextView. Làm cách nào tôi có thể đặt văn bản bên phải Switch.Làm thế nào để thay đổi Chuyển văn bản sang bên phải trong Android?

+0

Sử dụng thuộc tính xml như android: textOn và android: textOff trong xml của bạn. –

+1

@JiteshDalsaniya mặc định chuyển đổi có chuyển đổi văn bản bên trái và chuyển đổi có bên phải. Tôi đang mong đợi chuyển đổi bên trái và chuyển đổi văn bản bên phải. Bạn có thể xóa ngay bây giờ không? những gì đang mong đợi –

Trả lời

-2

tôi đã có thể để đạt được nhìn Tôi muốn có một sự kết hợp của:

  • chiều rộng (120dp)

  • đệm (75dp)

  • và switchpadding (-90dp)

enter image description here

8

enter image description here

Bỏ nó trong bố cục khác không có giá trị văn bản và thêm một lần xem văn bản. xem mã của tôi:

<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

Hoạt động tuyệt vời, nhưng làm thế nào để thực hiện khi nó được nhấp vào 'Lời nhắc họp' để chuyển điều khiển' Chuyển đổi ', Không có logic mã trong hoạt động? Ví dụ: 'for =" @ id/remind "" – mihkov

2

Thêm mã này trong hồ sơ 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" /> 

Trong hoạt động Hoặc Fragment:

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

Tôi nghĩ hữu ích của nó đối với bạn .... Cảm ơn bạn