2016-06-10 26 views
0

我已经在android中实现了一个自定义开关(如iOS)。它在xhdpi设备(Nexus 4,moto g3)中运行良好。但是Nexus S中的开关轨道宽度不能缩短太长。不能减小开关的大小android

custom_switch_thumb

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
<item android:state_checked="true"> 
    <shape 
     android:shape="rectangle" 
     android:visible="true" 
     android:dither="true" 
     android:useLevel="false"> 
     <gradient 
      android:startColor="#66AAFF00" 
      android:endColor="#6600FF00" 
      android:angle="270"/> 
     <corners 
      android:radius="20dp"/> 
     <size 
      android:width="30dp" 
      android:height="30dp" /> 
     <stroke 
      android:width="4dp" 
      android:color="#0000ffff"/> 
    </shape> 
</item> 
<item android:state_checked="false"> 
    <shape 
     android:shape="rectangle" 
     android:visible="true" 
     android:dither="true" 
     android:useLevel="false"> 
     <gradient 
      android:startColor="#ff0000" 
      android:endColor="#ff0000" 
      android:angle="270"/> 
     <corners 
      android:radius="20dp"/> 
     <size 
      android:width="30dp" 
      android:height="30dp" /> 
     <stroke 
      android:width="4dp" 
      android:color="#0000ffff"/> 
    </shape> 
</item> 

custom_switch_track

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" 
android:visible="true" 
android:dither="true" 
android:useLevel="false"> 
<gradient 
    android:startColor="#27170432" 
    android:endColor="#27170432" 
    android:angle="270"/> 
<corners 
    android:radius="20dp"/> 
<size 
    android:width="60dp" 
    android:height="30dp" /> 
</shape> 

开关置于布局

<Switch 
      android:id="@+id/notification_client_switch" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_marginLeft="8dp" 
      android:gravity="center" 
      android:showText="true" 
      android:switchMinWidth="0dp" 
      android:switchTextAppearance="@style/SwitchTextAppearance" 
      android:textOff="@string/no_label" 
      android:textOn="@string/yes_label" 
      android:thumb="@drawable/custom_switch_selector" 
      android:track="@drawable/custom_switch_track" /> 

我改变了轨道宽度和切换“android:switchminwidth”不同的值,但没有发现效果。

+0

可以使用切换按钮插入交换机。 – Ankur1994a

+0

@Tijo Joseph你想要'Ios'类型'switch'。 – Ironman

+0

@ Ankur1994a您可以发布切换按钮代码 –

回答

0

代码的toogle按钮

<ToggleButton 
      android:id="@+id/notification_client_switch" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/toggleBackground" 
      android:checked="true" 
      android:textOff=" " 
      android:textOn=" " /> 

XML为您绘制toggleBackground

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