android中ToggleButton的使用

简介:

这个小例子主要用来演示ToggleButton的基本使用。效果大致是一开始界面是垂直布局的,当点击ToggleButton按钮的时候,布局变为水平方向的

大致的代码贴一下吧,其中main.xml代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<? xml  version="1.0" encoding="utf-8"?>
< LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="vertical" >
 
     < ToggleButton
         android:id="@+id/toggleButton1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:checked="true"
         android:textOff="横向排列"
         android:textOn="纵向排列" />
 
     < LinearLayout
         android:id="@+id/lLayout"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="vertical" >
 
         < Button
             android:id="@+id/button1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="Button" />
 
         < Button
             android:id="@+id/button2"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="Button" />
 
         < Button
             android:id="@+id/button3"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="Button" />
     </ LinearLayout >
 
 
</ LinearLayout >

  AndroidDemo4Activity.java代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package  android.demo;
 
import  android.app.Activity;
import  android.os.Bundle;
import  android.widget.CompoundButton;
import  android.widget.CompoundButton.OnCheckedChangeListener;
import  android.widget.LinearLayout;
import  android.widget.ToggleButton;
 
public  class  AndroidDemo4Activity  extends  Activity {
     /** Called when the activity is first created. */
     @Override
     public  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.main);
         ToggleButton toggle=(ToggleButton)findViewById(R.id.toggleButton1);
         final  LinearLayout layout=(LinearLayout)findViewById(R.id.lLayout);
         toggle.setOnCheckedChangeListener( new  OnCheckedChangeListener() {
             
             @Override
             public  void  onCheckedChanged(CompoundButton arg0,  boolean  arg1) {
                 if (arg1){
                     //设置垂直布局
                     layout.setOrientation( 1 );
                 } else {
                     //设置水平布局
                     layout.setOrientation( 0 );
                 }
                 
             }
         });
     }
}
目录
相关文章
|
4月前
|
XML Android开发 数据格式
[Android]Toolbar
[Android]Toolbar
28 0
|
Android开发
Android之FloatingActionButton使用
Android之FloatingActionButton使用
174 0
Android之FloatingActionButton使用
|
Android开发 数据格式 XML
|
Android开发 数据格式 XML
|
Android开发
Android PopupWindow使用
1、含义        写的第一篇技术文章还有点小激动,话说popupwindow顾名思义就是类似于对话框样式布局显示,但是却有别于对话框,到底哪里有区别呢,还请客官您细细观看。
918 0