android圆角按钮自定义
来源:互联网 发布:大数据产品总监 编辑:程序博客网 时间:2024/06/05 09:31
使用xml定义一个圆角矩形,外围轮廓线实线、内填充渐变色, drawablle文件下建立btn_bg.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">//设置圆角按钮
<solid android:color="#A2B5CD"/>//定义按钮的背景颜色
<corners android:radius="15dip"/>//设置按钮每个角的弧形角度
<padding//按钮文字与边界的距离
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp"/>
</shape>
shape控件里面还包含一些属性。
- <?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="#FFEC7600" />
- <corners
- android:topLeftRadius="5dip"
- android:topRightRadius="5dip"
- android:bottomLeftRadius="5dip"
- android:bottomRightRadius="5dip" />
- </shape>
- </item>
- <item android:top="1px" android:bottom="1px" android:left="1px" android:right="1px">
- <shape>
- <gradient
- android:startColor="#FFEC7600" android:endColor="#FFFED69E"
- android:type="linear" android:angle="90"
- android:centerX="0.5" android:centerY="0.5" />
- <corners
- android:topLeftRadius="5dip"
- android:topRightRadius="5dip"
- android:bottomLeftRadius="5dip"
- android:bottomRightRadius="5dip" />
- </shape>
- </item>
- </layer-list>
接下来,同样在drawable文件下建立btn_bg_new.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 按下时背景显示白色,松开透明-->
<item android:state_pressed="true" android:drawable="@color/bt_bg"/>
<item android:state_focused="true" android:drawable="@color/bt" />
<item android:drawable="@drawable/btn_bg" />
</selector>
main.xml文件中button应用自定义代码效果:
<Button
android:id="@+id/button_single"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/btn_bg_new"
android:text="@string/button_single" />
<Button
android:id="@+id/button_two"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:layout_gravity="center"
android:background="@drawable/btn_bg_new"
android:text="@string/button_two" />
<Button
android:id="@+id/button_risk"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:layout_gravity="center"
android:background="@drawable/btn_bg_new"
android:text="@string/button_risk" />
效果图如下:
- Android 自定义圆角按钮
- android自定义圆角按钮
- android圆角按钮自定义
- android自定义圆角动画按钮
- android自定义圆角动画按钮
- Android 自定义UI圆角按钮
- Androidの自定义圆角按钮button
- Android Shape自定义纯色圆角按钮
- Android 自定义UI圆角按钮
- Android Shape自定义圆角按钮
- Android Shape自定义纯色圆角按钮
- Android Shape自定义纯色圆角按钮
- Android Shape自定义纯色圆角按钮
- Android Shape自定义纯色圆角按钮
- Android自定义按钮之圆角矩形
- Android Shape自定义纯色圆角按钮
- 自定义圆角按钮
- Android开发之Shape自定义圆角按钮
- 如何写软件设计文档
- android开发笔记之多媒体—SurfaceView
- Spring基于事件驱动模型的订阅发布模式代码实例详解
- Java - L1-3. 个位数统计
- java 过滤器(Filter)学习笔记
- android圆角按钮自定义
- HZAU 1012 The Same Color
- 保障分布式系统数据一致性
- 120. Triangle
- ScrollView嵌套ListView
- oracle数据库登陆、修改用户密码
- poj 2912 Rochambeau
- 设计模式之单例模式
- hdu1255 覆盖的面积(矩形面积交+扫描线)