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. gravity和layout_gravity的区别
  2. android设置Activity背景色为透明的2种方法
  3. Android基础UI篇------TextView及其子类
  4. EditText属性大全
  5. 对android的android:taskAffinity初识
  6. Android:Gravity控制格式
  7. android Button 切换背景,实现动态按钮和按钮颜色渐变
  8. Android中RemoteViews的实现
  9. 关于progressbar进度条的显示风格及一些属性的应用

随机推荐

  1. 谷歌前实习生解释 Android不如苹果iOS原
  2. 任务
  3. Handler初探
  4. Android技术路线图
  5. [置顶] 学坏
  6. Android内存泄漏终极解决篇(下)
  7. Android(安卓)6.0(权限申请)反射和注解实现
  8. SD功能移植
  9. Android集成讯飞语音、百度语音、阿里语
  10. android中屏幕适配的方式