----------------------------------矩形或圆角类型(源代码下有属性解释)-----------------------------------------------------
一、shape 样式:(在drawable新建--》new--》Drawable resource file 并把原父级标签selector改为shape )
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#4ec5ff" /> <padding android:bottom="12dp" android:left="12dp" android:right="12dp" android:top="12dp" /> <corners android:radius="10dp" /> <stroke android:width="2dp" android:color="#ffffff" android:dashGap="270dp" /></shape>二、style 样式:
<style name="rectangle"> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">wrap_content</item> <item name="android:textSize">15sp</item> <item name="android:textColor">#ffffff</item> <item name="android:background">@drawable/buttonstyle</item></style>三、Button控件调用style样式:
<?xml version="1.0" encoding="utf-8"?><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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.ly.blogtest.MainActivity"> <Button android:id="@+id/button" style="@style/rectangle" android:text="@string/btn1" />
</RelativeLayout>
----------------------------------矩形或圆角类型-----------------------------------------------------
----------------------------------android:shape属性指定形状------------------------------
- rectangle: 矩形,默认的形状,可以画出直角矩形、圆角矩形、弧形等
- oval: 椭圆形,用得比较多的是画正圆
- line: 线形,可以画实线和虚线
- ring: 环形,可以画环形进度条
----------------------------------android:shape属性指定形状------------------------------
----------------------------------shape的属性标签-----------------------------------------
<shape> <!-- 实心 --> <solid android:color="#ff9d77"/> <!-- 渐变 --> <gradient android:startColor="#ff8c00" android:endColor="#FFFFFF" android:angle="270" /> <!-- 描边 --> <stroke android:width="2dp" android:color="#dcdcdc" /> <!-- 圆角 --> <corners android:radius="2dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape>
solid: 设置形状填充的颜色,只有android:color一个属性
- android:color 填充的颜色
padding: 设置内容与形状边界的内间距,可分别设置左右上下的距离
- android:left 左内间距
- android:right 右内间距
- android:top 上内间距
- android:bottom 下内间距
gradient: 设置形状的渐变颜色,可以是线性渐变、辐射渐变、扫描性渐变
- android:type 渐变的类型 linear 线性渐变,默认的渐变类型
- radial 放射渐变,设置该项时,android:gradientRadius也必须设置
- sweep 扫描性渐变
corners: 设置圆角,只适用于rectangle类型,可分别设置四个角不同半径的圆角,当设置的圆角半径很大时,比如200dp,就可变成弧形边了
- android:radius 圆角半径,会被下面每个特定的圆角属性重写
- android:topLeftRadius 左上角的半径
- android:topRightRadius 右上角的半径
- android:bottomLeftRadius 左下角的半径
- android:bottomRightRadius 右下角的半径
stroke: 设置描边,可描成实线或虚线。
- android:color 描边的颜色
- android:width 描边的宽度
- android:dashWidth 设置虚线时的横线长度
- android:dashGap 设置虚线时的横线之间的距离
----------------------------------shape的属性标签-----------------------------------------