android中样式和自定义button样式

一万年来谁著史,三千里外欲封侯。这篇文章主要讲述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>

 
从以上代码可以看出,应用方式为@style/testStyle。
【android中样式和自定义button样式】 





    推荐阅读