(一)

1.效果图

  

2. activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.app4.MainActivity"
android:orientation="vertical"> <ToggleButton
android:id="@+id/tb_button"
android:textOn="纵向布局"
android:textOff="横向布局"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <LinearLayout
android:id="@+id/ll"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:text="按钮1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="按钮2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="按钮3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>

3.MainActivity.java

 package com.example.app4;

 import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ToggleButton; public class MainActivity extends AppCompatActivity {
private ToggleButton toggleButton;
private LinearLayout linearLayout; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton = (ToggleButton) findViewById(R.id.tb_button);
linearLayout = (LinearLayout) findViewById(R.id.ll); toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
//设置纵向布局
linearLayout.setOrientation(LinearLayout.VERTICAL);
toggleButton.setChecked(true);
}else {
//设置纵向布局
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
toggleButton.setChecked(false);
}
}
});
}
}

(二)

1.效果图

    

2.activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.app4.MainActivity"
android:orientation="vertical"> <ToggleButton
android:id="@+id/tb_button"
android:textOn="纵向布局"
android:textOff="横向布局"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Switch
android:id="@+id/sw"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <LinearLayout
android:id="@+id/ll"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:text="按钮1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="按钮2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="按钮3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>

3.MainActivity.java

 package com.example.app4;

 import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.ToggleButton; public class MainActivity extends AppCompatActivity {
private ToggleButton toggleButton;
private LinearLayout linearLayout;
private Switch aSwitch; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton = (ToggleButton) findViewById(R.id.tb_button);
linearLayout = (LinearLayout) findViewById(R.id.ll);
aSwitch = (Switch) findViewById(R.id.sw); MyListener myListener = new MyListener(); toggleButton.setOnCheckedChangeListener(myListener);
aSwitch.setOnCheckedChangeListener(myListener); /*toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
//设置纵向布局
linearLayout.setOrientation(LinearLayout.VERTICAL);
toggleButton.setChecked(true);
}else {
//设置纵向布局
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
toggleButton.setChecked(false);
}
}
});*/
} class MyListener implements CompoundButton.OnCheckedChangeListener{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
//设置纵向布局
linearLayout.setOrientation(LinearLayout.VERTICAL);
toggleButton.setChecked(true);
aSwitch.setChecked(true);
}else {
//设置纵向布局
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
toggleButton.setChecked(false);
aSwitch.setChecked(false);
}
}
}
}

ToggleButton控件,Switch控件的更多相关文章

  1. Switch控件详解

    Switch控件详解 原生效果 5.x 4.x 布局 <Switch android:id="@+id/setting_switch" android:layout_widt ...

  2. 在tableviewcell里面嵌入switch控件以及如何获取switch控件数据

    主要是通过cell.accessoryView来添加switch控件- (UITableViewCell *)tableView:(UITableView *)tableView cellForRow ...

  3. ToggleButton开关状态按钮控件

    ToggleButton开关状态按钮控件 一.简介 1. 2.ToggleButton类结构 父类是CompoundButton,引包的时候注意下 二.ToggleButton开关状态按钮控件使用方法 ...

  4. Android Studio中Switch控件有关 textOn 和 textOff 用法

    •属性 textOn:控件打开时显示的文字 textOff:控件关闭时显示的文字 showText:设置是否显示开关上的文字(API 21及以上) •用法 <?xml version=" ...

  5. 【原创】自己动手写控件----XSmartNote控件

    一.前面的话 在上一篇博文自己动手写工具----XSmartNote [Beta 3.0]中,用到了若干个自定义控件,其中包含用于显示Note内容的简单的Label扩展控件,用于展示标签内容的labe ...

  6. WPF进阶技巧和实战03-控件(1-控件及内容控件)

    所有控件都继承自System.Windows.Controls.Control类,这个类添加一些基本结构: 设置控件内容对齐方式 (HorizontalContentAlignment,Vertica ...

  7. winform用户控件、动态创建添加控件、timer控件、控件联动

    用户控件: 相当于自定义的一个panel 里面可以放各种其他控件,并可以在后台一下调用整个此自定义控件. 使用方法:在项目上右键.添加.用户控件,之后用户控件的编辑与普通容器控件类似.如果要在后台往窗 ...

  8. paip.gui控件tabs控件加载内容的原理以及easyui最佳实现

    paip.gui控件tabs控件加载内容的原理以及easyui最佳实现 //////////////tabs控件的加载 同form窗体一样,俩个方式 两个方式:一个是url,简单的文本可以使用这个,不 ...

  9. zw版【转发·台湾nvp系列Delphi例程】Delphi 使用 HALCON库件COM控件数据格式转换

    zw版[转发·台湾nvp系列Delphi例程]Delphi 使用 HALCON库件COM控件数据格式转换 Delphi 使用 HALCON库件COM控件数据格式转换,与IHObjectX接口有关 va ...

随机推荐

  1. rman备份与异机恢复

    一.rman备份脚本并为定时任务 #!/bin/bashsource ~/.bash_profileexport LANG=en_USBACKUP_DATE=`date +%d`#RMAN_LOG_F ...

  2. 网络编程:connect函数

    TCP客户用connect函数来建立与TCP服务器的连接: cpp #include<sys/socket.h> int connect(int sockfd, const struct ...

  3. Java并发(5)- ReentrantLock与AQS

    引言 在synchronized未优化之前,我们在编码中使用最多的同步工具类应该是ReentrantLock类,ReentrantLock拥有优化后synchronized关键字的性能,又提供了更多的 ...

  4. 【bzoj3510】首都 LCT维护子树信息(+启发式合并)

    题目描述 在X星球上有N个国家,每个国家占据着X星球的一座城市.由于国家之间是敌对关系,所以不同国家的两个城市是不会有公路相连的. X星球上战乱频发,如果A国打败了B国,那么B国将永远从这个星球消失, ...

  5. BroadCastRecieve

    首先介绍一下BroadCastRecieve有几种: 1.无序广播(普通广播):sendBroadcast()方式 2.有序广播:sendOrderedBroadcast()方式 3.粘性广播:sen ...

  6. 使用dd命令克隆整个系统

    神奇的ghost的原理是什么呢?不就是数据复制吗?Linux下的dd命令不就是最强大的数据复制工具!          既然如此,我为什么要使用g4l这样复杂的工具呢?一条dd命令不就可以帮我实现任意 ...

  7. SUSE LINUX 11忘记密码的解决方法

    忘记ROOT的密码 1.重新启动机器,在出现grub引导界面后,在启动linux的选项里加上init=/bin/bash,通过给内核传递init=/bin/bash参数使得OS在运行login程序之前 ...

  8. SuSE Linux修改网卡配置

    http://blog.chinaunix.net/uid-20765159-id-1850854.html SuSE Linux修改网卡配置             2010 SuSE linux ...

  9. 基于iscroll实现下拉和上拉刷新

    在原生APP的开发中,有一个常见的功能,就是下拉刷新的功能,这个想必大家都是知道的,但是原生APP的开发,有一个很大的问题就是,你每次更新一些功能,就要用户重新下载一次版本,尤其是在iOS系统中,新版 ...

  10. Hibernate多对多两种情况

    Hibernate在做多对多映射的时候,除了原先的两张表外,会多出一个中间表做关联,根据中间表的会有两种不同的配置情况: 1.中间表不需要加入额外数据. 2.中间表有其他字段,需记录额外数据. 下面, ...