扣扣技术交流群:460189483 目录: 简单设置: <pre name="code" class="html"> <Switch android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="" android:textOn="" android:switchMinWidth="120dp" android:thumb="@android:color/transparent" android:track="@drawable/switch_track" /> <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android=""> <item android:drawable="@drawable/switch_close" android:state_checked="false" /> <item android:drawable="@drawable/switch_open" android:state_checked="true" /> </selector>
效果展示: 这里layout_width:这能设置整个布局的宽度,不能设置具体的Switch的大小,需要使用switchMinWidth属性来设置。 thumb:文字所携带的背景,设置为背景色进行隐藏。不设置会出现一个背景框。 track:设置开关的背景图片,类似于button的background。 textoff、texton:设置开关时的文字显示。 3.简单使用 3.2)主布局java类 package com.example.aswitch; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.SwitchCompat; import android.widget.CompoundButton; import android.widget.Switch; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{ private Switch aSwitch; private SwitchCompat aSwitchCompat; private TextView text1,text2,switchText,switchCompatText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //实例化 aSwitch = (Switch) findViewById(R.id.switch1); aSwitchCompat = (SwitchCompat) findViewById(R.id.switch_compat); text1 = (TextView) findViewById(R.id.text); text2 = (TextView) findViewById(R.id.text1); //设置Switch事件监听 aSwitch.setOnCheckedChangeListener(this); aSwitchCompat.setOnCheckedChangeListener(this); } /* 继承监听器的接口并实现onCheckedChanged方法 * */ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { switch (buttonView.getId()){ case R.id.switch1: if(isChecked){ text1.setText("开"); }else { text1.setText("关"); } break; case R.id.switch_compat: if(isChecked){ text2.setText("开"); }else { text2.setText("关"); } break; default: break; } } }3.3)截图效果 4.更改默认Switch的样式 4.1)在布局文件中通过android:theme="@style/mySwitch"设置 <android.support.v7.widget.SwitchCompat android:layout_marginTop="10dp" android:layout_below="@+id/switch_compat_tv" android:id="@+id/switch_compat" android:typeface="normal" android:theme="@style/mySwitch" android:switchMinWidth="40dp" android:switchPadding="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" />5.自定义Switch 5.3)实现track_selector.xml <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android=""> <!--打开时switch轨迹图片--> <item android:state_pressed="true" android:drawable="@drawable/track_on" /> <!--按压时switch轨迹图片--> <item android:state_checked="true" android:drawable="@drawable/track_press" /> <!--正常状态switch轨迹图片--> <item android:drawable="@drawable/track_nomal" /> </selector>5.4)主布局actiity_second.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="" xmlns:tools="" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="" 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.aswitch.SecondActivity"> <TextView android:id="@+id/CustomSwitchCompat_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="CustomSwitchCompat:" /> <android.support.v7.widget.SwitchCompat android:layout_marginTop="10dp" android:layout_below="@+id/CustomSwitchCompat_tv" android:id="@+id/CustomSwitchCompat" android:layout_width="wrap_content" android:minWidth="40dp" android:minHeight="20dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/custom_result" android:layout_marginTop="10dp" android:layout_below="@+id/CustomSwitchCompat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> </RelativeLayout>5.5)主布局java类SecondActivity.java package com.example.aswitch; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.SwitchCompat; import android.widget.CompoundButton; import android.widget.Switch; import android.widget.TextView; public class SecondActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{ private SwitchCompat customSwitchCompat; private TextView custom_result,CustomSwitchCompat_tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); //实例化 customSwitchCompat = (SwitchCompat) findViewById(R.id.CustomSwitchCompat); custom_result = (TextView) findViewById(R.id.custom_result); //设置自定义的thumb和track customSwitchCompat.setThumbResource(R.drawable.thumb_selector); customSwitchCompat.setTrackResource(R.drawable.track_selector); //设置Switch事件监听 customSwitchCompat.setOnCheckedChangeListener(this); } /* 继承监听器的接口并实现onCheckedChanged方法 * */ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ custom_result.setText("开"); }else { custom_result.setText("关"); } } }ps:其实自定义的途径还可以通过shape的绘制和java代码绘制,在这里就不详细说了 |