自定义按钮android
1. 如何自定义android Button样式
亲,可以用到Drawable中的shape哦,给你一个demo
java"><?xmlversion="1.0"encoding="utf-8"?>
<selectorxmlns:android="
<itemandroid:state_focused="false">
<shape>
<solidandroid:color="@color/find_passwordbar_bg"/>
<strokeandroid:width="0.5dp"android:color="#C8C8C8"/>
</shape>
</item>
<itemandroid:state_focused="true">
<shape>
<solidandroid:color="@color/find_passwordbar_bg"/>
<strokeandroid:width="0.5dp"android:color="@color/main_color"/>
</shape>
</item>
</selector>
各个属性的介绍
solid:实心,就是填充的意思
android:color指定填充的颜色
gradient:渐变
android:startColor和android:endColor分别为起始和结束颜色,ndroid:angle是渐变角度,必须为45的整数倍。
另外渐变默认的模式为android:type="linear",即线性渐变,可以指定渐变为径向渐变,android:type="radial",径向渐变需要指定半径android:gradientRadius="50"。
stroke:描边
android:width="2dp"描边的宽度,android:color描边的颜色。
我们还可以把描边弄成虚线的形式,设置方式为:
android:dashWidth="5dp"
android:dashGap="3dp"
其中android:dashWidth表示'-'这样一个横线的宽度,android:dashGap表示之间隔开的距离。
corners:圆角
android:radius为角的弧度,值越大角越圆。
我们还可以把四个角设定成不同的角度,方法为:
<corners
android:topRightRadius="20dp"右上角
android:bottomLeftRadius="20dp"右下角
android:topLeftRadius="1dp"左上角
android:bottomRightRadius="0dp"左下角
/>
我自己写的一个按钮,效果就像图中所示,用的Shape
新建后存放位置在res/drawable下
希望能帮到你,还望采纳
2. 如何自定义android Button样式
在windows7操作系统Android studio中按照如下方法定义button的样式。
1、首先使用Android studio创建一个项目,项目结构如下:
3. 如何自定义android Button样式
第一步 使用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="#FFEC7600"><corners android:bottomleftradius="5dip" android:bottomrightradius="5dip" android:topleftradius="5dip" android:toprightradius="5dip"></corners></solid></shape></item><item android:bottom="1px" android:left="1px" android:right="1px" android:top="1px"><shape><gradient android:angle="90" android:centerx="0.5" android:centery="0.5" android:endcolor="#FFFED69E" android:startcolor="#FFEC7600" android:type="linear"><corners android:bottomleftradius="5dip" android:bottomrightradius="5dip" android:topleftradius="5dip" android:toprightradius="5dip"></corners></gradient></shape></item></layer-list>
第二步写一个选择器bg_button_select.xml
第三部,button中引用android:background="@drawable/bg_button_select"
4. 如何自定义android Button样式
主要有2种方式进行自定义android button 的样式。
【主要方式】
自定义button样式。
自定义style样式。
【原理】
通过android sdk提供的方案进行自定义相关的android button的样式。
【详细实现步奏】
1.自定义button样式
主要是通过自定义背景图或者通过自定义selector的方式形成button的样式效果。
1.1图片的方式:
<?xmlversion="1.0"encoding="utf-8"?>
<selectorxmlns:android="http://schemas.android.com/apk/res/android">
<itemandroid:state_pressed="true"android:drawable="@drawable/a1";/>
<itemandroid:state_focused="true"android:drawable="@drawable/a2";/>
<itemandroid:drawable="@drawable/a1";/>
</selector>
xm布局文件使用
<Buttonandroid:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_style">
</Button>
1.2 自定义selector的方式,selector 种使用以下属性进行自定义自己所需要的按钮样式。
gradient 主体渐变,startColor开始颜色,endColor结束颜色 , angle开始渐变的角度(值只能为90的倍数,0时为左到右渐变,90时为下到上渐变,依次逆时针类推) stroke 边框 width 边框宽度,color 边框颜色, corners 圆角 radius 半径,0为直角, padding text值的相对位置。
2.自定义style样式
如下样式:
<resourcesxmlns:android="http://schemas.android.com/apk/res/android">
<stylename="AppBaseTheme"parent="android:Theme.Light">
</style>
<stylename="AppTheme"parent="AppBaseTheme">
</style>
<stylename="btnStyle">
<itemname="android:textSize">30px</item>
<itemname="android:width">15px</item>
<itemname="android:height">15px</item>
</style>
</resources>
xml文件中引用
<Button
style="@style/btnStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"/>
【最后】
2种方式组合使用效果更佳,效率更高,修改方便。
5. 如何自定义android Button样式
1)自定义button样式
一、采用图片方式
首先新建Android XML文件,类型选Drawable,根结点选selector,自定义一个文件名。
随后,开发环境自动在新建的文件里加了selector结点,我们只需要在selector结点里写上三种状态时显示的背景图片(按下、获取焦点,正常)即可。具体如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/play_press" ;/>
<item android:state_focused="true" android:drawable="@drawable/play_press" ;/>
<item android:drawable="@drawable/play" ;/>
</selector>
注:这里获取焦点跟点击时显示的是同一张图片,必须严格照上面的顺序写,不可倒。
最后,只要在布局时写Button控件时应用到Button的Background属性即可,如:
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_style">
</Button>
二、采用自定义方式
在源代码中,只需要修改button_style文件,同样三种状态分开定义:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<gradient android:startColor="#0d76e1"
android:endColor="#0d76e1"
android:angle="270" />
<stroke android:width="1dip" android:color="#f403c9" />
<corners android:radius="2dp" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</shape>
</item>
<item android:state_focused="true">
<shape>
<gradient android:startColor="#ffc2b7" android:endColor="#ffc2b7" android:angle="270" />
<stroke android:width="1dip" android:color="#f403c9" />
<corners android:radius="2dp" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient android:startColor="#000000" android:endColor="#ffffff"
android:angle="180" />
<stroke android:width="1dip" android:color="#f403c9" />
<corners android:radius="5dip" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</shape>
</item>
</selector>
注:代码中的各属性含义为:
gradient 主体渐变
startColor开始颜色,endColor结束颜色 ,
angle开始渐变的角度(值只能为90的倍数,0时为左到右渐变,90时为下到上渐变,依次逆时针类推)
stroke 边框 width 边框宽度,color 边框颜色
corners 圆角 radius 半径,0为直角
padding text值的相对位置
2)自定义style样式
一、在style.xml中自定义样式
以自定义text文本大小和颜色为例,自定义一个名称为"testStyle"的style代码如下:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
</style>
<style name="testStyle">
<item name="android:textSize">30px</item>
<item name="android:textColor">#1110CC</item>
<item name="android:width">150dip</item>
<item name="android:height">150dip</item>
</style>
</resources>
二、在layout文件中引用自定义的"testStyle"的style样式
<RelativeLayout 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"
tools:context=".MainActivity" >
<TextView
style="@style/testStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
</RelativeLayout>