UI组件之TextView及其子类(三)ToggleButton和Switch
ToggleButton、Switch、CheckBox和RadioButton都是继承自android.widget.CompoundButton,意思是可选择的,因此它们的使用方法都非常类似。
CompoundButton有两个状态,各自是checked和not checked。
ToggleButton的属性:
Switch组件的属性:
android:thumb是选中时的背景
例:开关button控制布局方向
main.xml
<?xml version="1.0" encoding="utf-8"? >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
android:textOff="横向排列"
android:textOn="纵向排列" />
<!-- android:thumb="@drawable/check" 使用自己定义的drawable对象绘制开关按钮 -->
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Switch"
android:textOff="横向排列"
android:textOn="纵向排列"
android:thumb="@drawable/check" />
<LinearLayout
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" /> <Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3" />
</LinearLayout>
</LinearLayout>
MainActivity.java
ToggleButton toggle;
Switch switcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggle=(ToggleButton) findViewById(R.id.toggleButton1);
switcher=(Switch) findViewById(R.id.switch1); final LinearLayout test=(LinearLayout) findViewById(R.id.root);
//ToggleButton和Switch的监听接口和复选框CheckButton的一样
CompoundButton.OnCheckedChangeListener listener=new CompoundButton.OnCheckedChangeListener(){ @Override
public void onCheckedChanged(CompoundButton button, boolean checkedId) {
// TODO Auto-generated method stub
if(checkedId){
//1表示垂直布局,0表示水平布局
test.setOrientation(1);
}else{
test.setOrientation(0);
}
}
}; toggle.setOnCheckedChangeListener(listener);
switcher.setOnCheckedChangeListener(listener);
}
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
以后要学会自己定制跟家美观的组件
推荐几个好的博客:
UI组件之TextView及其子类(三)ToggleButton和Switch的更多相关文章
- 第2组UI组件:TextView及其子类
1 TextView及其子类的继承关系 TextView直接继承自View,是EditView与Button两个类的父类,如下为TextView各子类继承关系. 2 个UI的样式图 CheckedTe ...
- UI组件之TextView及其子类(五)计时器Chronometer
Chronometer直接继承了TextView组件,它会显示一段文本,显示从某个事实上时间開始.一共过了多长时间.我们看Chronometer的源代码: watermark/2/text/aHR0c ...
- UI组件之TextView及其子类(一)TextView和EditText
先来整理一下TexView,EditView的使用方法. Textview是最主要的组件.直接继承了View,也是众多组件的父类.所以了解她的属性会对学习其它组件非常有帮助. TextView的属性: ...
- 第16讲- UI组件之TextView
第16讲 UI组件之TextView Android系统所有UI类都是建立在View和ViewGroup这两类的基础上的. 所有View的子类称为widget:所有ViewGroup的子类称为Layo ...
- 第6组UI组件:ViewAnimator及其子类
ViewAnimator是一个基类,它继承了FrameLayout,因此它表现出FrameLayout的特征,可以将多个View组件“叠”在一起.ViewAnimator额外增加的功能正如它的名字所暗 ...
- Android用户界面 UI组件--TextView及其子类(三) EditView以及各种Span文字样式讲解
EditView和TextView的用法差不多,只是文字可编辑 小技巧: 设置EditText隐藏键盘 setInputType(0); 设置EditText不被输入法遮盖 getWindow() ...
- UI组件之AdapterView及其子类(三)Spinner控件具体解释
Spinner提供了从一个数据集合中高速选择一项值的办法. 默认情况下Spinner显示的是当前选择的值.点击Spinner会弹出一个包括全部可选值的dropdown菜单或者一个dialog对话框,从 ...
- 第3组UI组件:AdapterView及其子类
1 AdapterView类简介 1.1 AdapterView组件是一组重要的组件,AdapterView本身是一个抽线类,实际使用更多的都是Adapter相关子类,AdapterView具有如下特 ...
- UI组件之AdapterView及其子类关系,Adapter接口及事实上现类关系
AdapterView本身是一个抽象基类,它派生的的子类在使用方法上十分类似.AdapterView直接派生的三个子类:AbsListView.AbsSpinner,AdapterViewAnimat ...
随机推荐
- linux 下find---xargs以及find--- -exec结合使用
例:删除/home/raven下,包括子目录里所有名为abc.txt的文件: find /home/raven -name abc.txt | xargs rm -rf 如果不使用xargs,则为: ...
- sync---强制将被改变的内容立刻写入磁盘
sync命令用于强制被改变的内容立刻写入磁盘,更新超块信息. 在Linux/Unix系统中,在文件或数据处理过程中一般先放到内存缓冲区中,等到适当的时候再写入磁盘,以提高系统的运行效率.sync命令则 ...
- 【习题 8-9 1613】 K-Graph Oddity
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 感觉最大度数|1就是最多需要的个数了. 就贪心一下. 然后模拟染色的过程就可以了. (贪心染色就可以了 (看看周围哪个颜色没有,就用 ...
- 【hihocoder 1122】二分图二•二分图最大匹配之匈牙利算法
[Link]:https://hihocoder.com/problemset/problem/1122 [Description] [Solution] 二分图匹配,匈牙利算法模板题; 这里我先把染 ...
- 思科模拟器之路由器-RIP-DNS解析server
思科三层交换机之下的局域网搭建,请看这. 接下来将解说怎样通过路由器的RIP协议来连接多个局域网. 并设置DNSserver. 1.路由器RIP配置 RIP协议有个非常致命的缺点:就是它是依据路径长短 ...
- Android开源图表库XCL-Charts版本号公布及展示页
XCL-Charts V2.1 Android开源图表库(XCL-Charts is a free charting library for Android platform.) XCL-Charts ...
- Activity转换为View和把图片转换为View
package com.example.viewpager01; import java.util.ArrayList; import java.util.List; import android.a ...
- <memory>(包括了auto_ptr,shared_ptr等各种指针)
Memory elements This header defines general utilities to manage dynamic memory: Allocators allocator ...
- WIN8.1 上安装 debian8.7 遇到的问题及解决方法
WIN8.1 上安装 debian8.7 遇到的问题及解决方法 参照百度经验 <win7下硬盘安装debian7> ( http://jingyan.baidu.com/article/8 ...
- Android HttpLoggingInterceptor的用法简介
该拦截器用于记录应用中的网络请求的信息. 示例 OkHttpClient client = new OkHttpClient(); HttpLoggingInterceptor logging = n ...