6.Android之switch和togglebutton按钮学习

时间:2023-03-09 02:59:26
6.Android之switch和togglebutton按钮学习

Switch和ToggleButton按钮在手机上也经常看到,比如手机设置里面wlan,蓝牙,gps开关等。

首先在工具上拖进一个switch和togglebutton开关按钮,如图

6.Android之switch和togglebutton按钮学习

生成xml代码如下:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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.switchtogglebutton.MainActivity" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="46dp"
android:text="Switch开关" /> <ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/switch1"
android:layout_below="@+id/switch1"
android:layout_marginTop="36dp"
android:text="ToggleButton" /> </RelativeLayout>

运行显示效果:

6.Android之switch和togglebutton按钮学习

6.Android之switch和togglebutton按钮学习