2016-11-04 4 views
2

Я создал файл макета с помощью переключателя. Теперь я хочу изменить форму его селектора. Также я хочу удалить текст на этом переключателе.Как изменить форму переключателя в android

В настоящее время он имеет прямоугольную форму. Я хочу изменить его на круговую форму.

This is my requirement

Это мой файл макета,

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_remind_me" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.pvt.RemindMeActivity"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="3"> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/remind_me_head_txt" 
       android:layout_marginTop="5dp" 
       android:textSize="20sp" 
       android:text="Example one" 
       android:textStyle="bold"/> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="120dp" 
      android:layout_weight="1"> 

      <Switch 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="2dp" 
       android:showText="false" 
       android:id="@+id/remind_me_switch"/> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:layout_weight="1"> 

      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="5dp" 
       android:id="@+id/remind_me_list_img" 
       android:src="@drawable/list_menu" /> 

     </LinearLayout> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="8dp"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/remind_me_title_txt" 
      android:text="Sample Head" 
      android:textSize="12sp"/> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="8dp"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/remind_me_content_txt" 
      android:text="Content" 
      android:textSize="12sp"/> 

    </LinearLayout> 

</LinearLayout> 

В настоящее время я не имею ни малейшего представления о том, как достичь этого.

Можем ли мы изменить форму переключателя? Является ли это возможным ?

+2

вы можете попробовать https://github.com/kyleduo/SwitchButton – USKMobility

ответ

1

Использование любых форм и стилей у, как для переключателя в андроида Эта библиотека поможет U много SwitchButton

3

Эти изменения формы в соответствии с устройствами, например, будут отображаться прямоугольными в Redmi Note и круговыми в Moto.

Хотя, для того, чтобы достичь этого, вы можете иметь реализацию тумблер, например, как показано ниже: Имея три стиля:

  • offToggle (Style Off)
  • onToggle (Style On)
  • custom_toggle (Style Custom Toggle)

Стиль Off

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval" 
    android:visible="true"> 
    <solid android:color="@android:color/holo_blue_dark" /> 
</shape> 

Стиль На

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval" 
    android:visible="true"> 
    <solid android:color="@android:color/black" /> 
</shape> 

Теперь коллективный стиль, который мы будем использовать в нашем макете пользовательских стилей Переключить

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/offToggle" android:state_enabled="false"/> 
    <item android:drawable="@drawable/onToggle" android:state_pressed="true"/> 
    <item android:drawable="@drawable/onToggle" android:state_checked="true"/> 
    <item android:drawable="@drawable/onToggle" android:state_focused="true"/> 
    <item android:drawable="@drawable/offToggle"/> 
</selector> 

Теперь просто использовать этот стиль в макете , например:

<ToggleButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textOn="" 
    android:textOff="" 
    android:background="@drawable/custom_toggle" /> 

Совет: Чтобы удалить текст с ключом/Переключить использовать android:textOn=""android:textOff="".

Смежные вопросы