ToggleButton 与Switch二者都是Button的子类,与RadioButton和CheckBox不同的是,ToggleButton 与Switch二者通常用作切换程序中的某种状态
ToggleButton 与Switch二者常用的xml属性是 android:checked;android:textOn;android:textOff;
而Switch常用的属性还有,android:thumb;
由于Switch是android4.0以后才有的版本,所以用该按钮的时候 一定要将项目的anroid版本调高一些,或者将AVD的API调高一些。才不会出现问题,本文利用这两个按钮来控制页面布局,这里用到了<LinearLayout/>
====================================================================================
xml代码


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 定义一个ToggleButton按钮 -->
<ToggleButton android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="横向排列"
android:textOn="纵向排列"
android:checked="true"
/>
<Switch android:id="@+id/switcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="横向排列"
android:textOn="纵向排列"
android:thumb="@drawable/check"
android:checked="true"/>
<!-- 定义一个可以动态改变方向的线性布局 -->
<LinearLayout android:id="@+id/test"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试按钮一"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"==将这三个按钮放在了一个LinearLayout中
android:text="测试按钮二"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试按钮三"
/>
</LinearLayout>
</LinearLayout>
==================================================================================
java代码
package org.crazyit.ui;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.ToggleButton;

/**
* Description:
* <br/>site: <a href="http://www.crazyit.org">crazyit.org</a>
* <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class ToggleButtonTest extends Activity
{
ToggleButton toggle;
Switch switcher;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
toggle = (ToggleButton)findViewById(R.id.toggle);
switcher = (Switch)findViewById(R.id.switcher);
final LinearLayout test = (LinearLayout)findViewById(R.id.test);
toggle.setOnCheckedChangeListener(new OnCheckedChangeListener()
{

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
//设置LinearLayout垂直布局
test.setOrientation(1);
Log.i("j","jhkjh");
}
else
{
//设置LinearLayout水平布局
test.setOrientation(0);
}

}
});
switcher.setOnCheckedChangeListener(new OnCheckedChangeListener()
{

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
//设置LinearLayout垂直布局
test.setOrientation(1);
Log.i("j","jhkjh");
}
else
{
//设置LinearLayout水平布局
test.setOrientation(0);
}

}
});
}
}

更多相关文章

  1. Android 五种布局简单介绍
  2. android Button 切换背景,实现动态按钮和按钮颜色渐变
  3. android点击按钮控制图片切换-kotlin
  4. 选择框在右边的单选按钮。。
  5. Android Layout Tricks #2: Reusing layouts(Android 布局技巧2:重
  6. 35、键盘布局的tableLayout备份
  7. 相对布局RelativeLayout
  8. android布局ui
  9. android : 圆角按钮 shape属性

随机推荐

  1. Android中实现双指缩放的功能
  2. Android内核开发实践
  3. Android之Goldfish
  4. Android日志分析
  5. adb错误:Failed to execute android comma
  6. 【Android】Android(安卓)SDK安装
  7. Android二维码扫描:基于barcodescanner
  8. android纹理图片的加载与修改
  9. Android热修复:Qfix方案的gradle实践
  10. 对《深入理解Android虚拟机》一书的知识