Android 样式 :shape selector 篇

2017-10-26  本文已影响301人  小武_2170

一个view控件的各种样式:包括控件间隔、文字大小和颜色、阴影、形状等等,这些样式主要通过shape、selector、layer-list、level-list、style、theme等组合实现。控件包括Button、EditText、ProgressBar、Toast、Checkbox等。

一 , shape

shape 用于形状和背景颜色的样式,分为以下四种形状。

android:shape属性:
效果属性:比如颜色,圆角:
rectangle 矩形列子:
效果图
  这是一个带圆角,渐变背景矩形;如果不要渐变效果,直接删掉,这里是为了综合运用。
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/orange"  />
    <padding
        android:left="@dimen/dp5"
        android:right="@dimen/dp5"/>
// 这里如果不需要四个角都是圆弧,可以自定义想要的角圆弧,具体属性看上面 (效果属性)
    <corners android:radius="@dimen/dp10" />
    <gradient
        android:angle="90"
        android:centerColor="@color/colorPrimaryDark"
        android:endColor="@color/orange"
        android:startColor="@color/colorAccent"
        android:type="linear" />
</shape>
line: 线形:

这里就不用给案例了,用上面的效果属性(stroke)实现,也可以用View来实现线.

oval 圆例子:
效果图
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/orange" />
    <gradient
        android:angle="90"
        android:centerColor="@color/colorPrimaryDark"
        android:endColor="@color/orange"
        android:startColor="@color/colorAccent"
        android:type="linear" />

    // 设置形状的大小,这个大小最好不要在这里设置,如果内容多大于设置的这个尺寸,就会被拉伸变形,在View控件设置大小
    <size
        android:width="@dimen/dp80"
        android:height="@dimen/dp80"
    />
</shape>
ring: 环形例子
效果图
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadiusRatio="3"
    android:shape="ring"
    android:thicknessRatio="9"
    android:useLevel="false">
    <solid android:color="@color/orange" />
// 如果不要这个属性,就是一个标准的环形, 可以通过android:type的三个属性设置不同的效果
    <gradient
        android:angle="90"
        android:endColor="@color/orange"
        android:startColor="@color/white"
        android:type="sweep" />
// 设置边线
    <stroke android:width="1dp"
        android:color="@color/black"/>

    // 设置形状的大小,这个大小最好不要在这里设置,如果内容多大于设置的这个尺寸,就会被拉伸变形,在View控件设置大小
    <size
        android:width="@dimen/dp80"
        android:height="@dimen/dp80" />
</shape>

二, selector

selector样式,一般用于改变样式状态,比如button按下改变背景,CheckBox选中状态, 是通过item标签中定义的属性改变的,可以添加一个或多个item子标签.

item常用属性

按钮背景

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/orange"/>
    <item android:drawable="@drawable/pressed_orange" android:state_pressed="true"/>
</selector>

按钮文本颜色

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@android:color/white" android:state_pressed="true" />
    <item android:color="@android:color/black" android:state_checked="false" />
</selector>

最后控件的引用

<Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="xiaowu"
            android:textColor="@drawable/text_color"
            android:background="@drawable/first_selector"/>

CheckBox 切换

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/item_uncheck" android:state_pressed="true" />
    <item android:drawable="@drawable/item_check" android:state_checked="true" />
    <item android:drawable="@drawable/item_uncheck" android:state_checked="false" />
</selector>
上一篇 下一篇

猜你喜欢

热点阅读