Android控件篇 Button(二)
来源:互联网 发布:12315可以投诉淘宝官方 编辑:程序博客网 时间:2024/06/16 22:16
1. Button样式
Button的样式可以分为3类:
(01) 使用Android系统自带的Button样式。
(02) 使用jpg/png等图片。
(03) 自定义shape。
1.1 使用Android系统自带的Button样式
在定义Button时,使用android自带的Button样式。如下所示:
<Button android:id="@+id/bt_style" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/bt_style" style="?android:attr/borderlessButtonStyle" android:textSize="12sp" />
说明:上面是创建一个无边框按钮。当然,也可以去掉style或将style换成其他的值。
(01) 若去掉的话,则使用Android默认的样式。
(02) 若要换成其他的值,可以在frameworks/base/core/res/res/values/public.xml中查看Button支持的style样式。
1.2 使用图片
首先,在定义Button时,通过"android:background"属性指定Button的背景图。
<Button android:id="@+id/bt_sync" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/bt_sync" android:background="@drawable/button_custom" android:textSize="12sp" />
说明:android:background="@drawable/button_custom",这意味着按钮的背景是drawable下的button_custom文件。下面是button_custom.xml的内容。
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Android系统在处理下面的状态时,是按照它们定义的先后顺序进行处理的 --> <!-- 按钮按下 --> <item android:drawable="@drawable/ic_sync_red" android:state_pressed="true" /> <!-- 按钮获取焦点 --> <item android:drawable="@drawable/ic_sync_green" android:state_focused="true" /> <!-- 默认 --> <item android:drawable="@drawable/ic_sync_grey" /></selector>
说明:需要注意的是,系统会根据状态定义的先后顺序对这些状态进行处理!ic_sync_red.png, ic_sync_green.png, ic_sync_grey.png是drawable目录下的三种图片。
1.3 自定义shape
关于自定义shape,这里给出3个示例进行介绍。
shape示例一
自定义Button背景:未按下的时候是灰色的圆角矩形,按下的时候是深灰色的圆角矩形。
button的定义如下
<Button android:id="@+id/bt_diy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_btn_gray_pressed_dkgray" android:text="shape_01" android:textSize="12sp" />
bg_btn_gray_pressed_dkgray.xml的内容如下
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="false"> <shape android:shape="rectangle"> <corners android:radius="8dip"/> <solid android:color="#ffcccccc"/> </shape> </item> <item android:state_pressed="true"> <shape android:shape="rectangle"> <corners android:radius="8dp"/> <solid android:color="#ff666666"/> </shape> </item></selector>
shape示例二
自定义Button背景:未按下的时候是黑边的透明圆角矩形框,按下的时候是黑边的灰色圆角矩形。
button的定义如下
<Button android:id="@+id/bt_diy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_btn_black_frame" android:text="shape_02" android:textSize="12sp" />
bg_btn_black_frame.xml的内容如下
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="false"> <shape android:shape="rectangle"> <corners android:radius="8dip"/> <stroke android:width="1dip" android:color="#ff000000" /> <solid android:color="#00ffffff"/> </shape> </item> <item android:state_pressed="true"> <shape android:shape="rectangle"> <corners android:radius="8dp"/> <stroke android:width="1dip" android:color="#ff000000" /> <solid android:color="#22000000"/> </shape> </item></selector>
shape示例三
自定义Button背景:红色圆环
button的定义如下
<Button android:id="@+id/bt_diy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_btn_ring" android:text="shape_03" android:textSize="12sp" />
bg_btn_black_frame.xml的内容如下
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item> <shape android:shape="rectangle" > <solid android:color="#ffffff" /> </shape> </item> <item> <!-- android:innerRadiusRatio="8" 表示"圆环半径=圆环宽度/8" android:thicknessRatio="10" 表示"圆环厚度=圆环宽度/10" --> <shape android:shape="ring" android:innerRadiusRatio="8" android:thicknessRatio="10" android:useLevel="false" > <solid android:color="#660000" /> <size android:height="32dip" android:width="32dip" /> </shape> </item></layer-list>
2. selector和shape
2.1 selector
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 按下状态:(01) true表示按下,例如,Button被按下; (02) false表示非按下。 --> <item android:state_pressed="true/false" android:drawable="@drawable/exam01" /> <!-- 聚焦状态:(01) true表示获取焦点; (02) false表示非获取焦点。 --> <item android:state_focused="true/false" android:drawable="@drawable/exam01" /> <!-- 选中状态:(01) true表示选中,例如,Tab被选中; (02) false表示非选中。 --> <item android:state_selected="true/false" android:drawable="@drawable/exam01" /> <!-- 可勾选状态:(01) true表示可勾选; (02) false表示不可勾选。 --> <item android:state_checkable="true/false" android:drawable="@drawable/exam01" /> <!-- 选中状态:(01) true表示选中,例如,checkbox被选中; (02) false表示非选中。 --> <item android:state_checked="true/false" android:drawable="@drawable/exam01" /> <!-- 使能状态:(01) true表示可用; (02) false表示不可用。 --> <item android:state_enabled="true/false" android:drawable="@drawable/exam01" /> <!-- 焦点状态:(01) true表示获取焦点; (02) false表示可获取焦点。 --> <item android:state_window_focused="true/false" android:drawable="@drawable/exam01" /></selector>
2.2 shape
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape=["rectangle" | "oval" | "line" | "ring"] > --- 默认为rectangle android:innerRadius="dimension" -- ring专用,内圆环的半径大小。设置innerRadius时,会忽略innerRadiusRatio android:innerRadiusRatio="float" -- ring专用,内圆环的半径比例。例如,比例为10表示"内圆环的半径大小=圆环宽度/10" android:thickness="dimension" -- ring专用,内圆环的厚度大小。设置thickness时,会忽略thicknessRatio android:thicknessRatio="float" -- ring专用,内圆环的厚度比例。例如,比例为10表示"内圆环的厚度大小=圆环宽度/10" android:useLevel="true|false" -- true表示将该形状当作LevelListDrawable <size -- 大小 android:width="dimension" android:height="dimension" /> <gradient -- 渐变 android:type=["linear" | "radial" | "sweep"] -- 渐变类型:线性,径向,扫描 android:angle="float" android:startColor="color" android:centerColor="color" -- 中心点颜色 android:endColor="color" android:centerX="float|fraction" -- 中心点X坐标 android:centerY="float|fraction" -- 中心点Y坐标 android:gradientRadius="float|fraction" -- 径向渐变的半径 android:useLevel=["true" | "false"] /> <corners -- shape=“rectangle”时使用, android:radius="dimension" -- 四个角的半径 android:topLeftRadius="dimension" android:topRightRadius="dimension" android:bottomLeftRadius="dimension" android:bottomRightRadius="dimension" /> <padding -- 内容与边框的距离 android:left="dimension" android:top="dimension" android:right="dimension" android:bottom="dimension" /> <solid -- 填充颜色 android:color="color" /> <stroke -- 指定边框 android:width="dimension" android:color="color" android:dashWidth="dimension" -- 虚线宽度 android:dashGap="dimension" /> -- 虚线间隔宽度</shape>
来源:https://wangkuiwu.github.io/2014/02/03/Button02/
- Android控件篇 Button(二)
- Android控件之二 (Button)
- 【android自定义控件】button样式自定义<二>
- Android控件篇 Button(一)
- android控件05 Button
- Android-Button控件基础
- Android控件之Button
- Android中的Button控件
- Android控件之Button
- android 控件 按钮(Button)
- android中Button控件
- Android控件之Button
- android控件Button
- Android控件之Button
- android控件02---Button
- android控件之Button
- .NET 自定义Button控件(二)
- Android 控件开发之Button
- lua中删除元素
- 如何部署web服务到tomcat中
- Taijan算法模板
- goahead上传文件的问题
- Gartner全球IAAS市场报告:阿里云进入全球前三
- Android控件篇 Button(二)
- Android Studio下JNI编程(引入外部或AS自行编译so文件)
- 一文理解深度学习,卷积神经网络,循环神经网络的脉络和原理3-残差神经网络
- 数据库事务操作
- ucos 获取查找最高优先级任务算法
- android学习笔记(二)
- 汉诺塔问题
- C++的STL之stack
- 【性能测试-jmeter】使用jmeter做dubbo服务线程同步并发测试(多图)