[Android进阶笔记]XML相关——Selector、Shape、Styles

app\src\main\res\drawable\mybtn_selector.xml
例子:

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/heart" />
    <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/heart_sel" />
    <!-- Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/heart_sel" />
    <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/heart_sel" />
    <!-- Pressed -->
    <item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/heart_sel" />
    <item android:state_pressed="true" android:drawable="@drawable/heart_sel" />
</selector>

各属性说明

android:state_pressed 是否按下,如一个按钮触摸或者点击。
android:state_focused 是否取得焦点,比如用户选择了一个文本框。
android:state_hovered 光标是否悬停,通常与focused state相同,它是4.0的新特性
android:state_selected 被选中,它与focus state并不完全一样,如一个list view 被选中的时候,它里面的各个子组件可能通过方向键,被选中了。
android:state_checkable 组件是否能被check。如:RadioButton是可以被check的。
android:state_checkedchecked了,如:一个RadioButton可以被check了。
android:state_enabled 能够接受触摸或者点击事件
android:state_activated 被激活
android:state_window_focused 应用程序是否在前台,当有通知栏被拉下来或者一个对话框弹出的时候应用程序就不在前台了

app\src\main\res\drawable\myshape.xml
转载自:http://blog.csdn.net/harvic880925/article/details/41850723
例子:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 圆角 -->
    <!-- 依次为统一半径和四个角的半径 -->
    <!-- 统一半径会被单独设置的覆盖 -->
    <corners
        android:radius="9dp"
        android:topLeftRadius="2dp"
        android:topRightRadius="2dp"
        android:bottomLeftRadius="2dp"
        android:bottomRightRadius="2dp"/>

    <!-- 渐变 -->
    <!--android:type=["linear" | "radial" | "sweep"] 线性渐变/放射渐变/横扫渐变 -->
    <!--android:startColor=#ff0000 起始、中间、结束颜色设置 -->
    <!--android:angle= n*45 渐变角度 为45度倍数  分别对应8个方向渐变 -->
    <!--android:useLevel=T or F 使用LevelListDrawable时设置为true。设为false时才有渐变效果 -->
    <!--android:centerX Y= 0~1 渐变中心X、Y的相对位置 0,0左上角 1,1右下角 -->
    <!--android:gradientRadius=1000 当type=radial时 设置渐变的半径 -->
    <gradient
        android:startColor="@android:color/white"
        android:centerColor="@android:color/black"
        android:endColor="@android:color/black"
        android:useLevel="false"
        android:angle="45"
        android:type="sweep"
        android:centerX="0"
        android:centerY="0"
        android:gradientRadius="90"/>

    <!-- 间隔 -->
    <!-- 各方向的间隔 -->
    <padding
        android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp"/>

    <!-- 大小 -->
    <!-- 宽度和高度 -->
    <size
        android:width="50dp"
        android:height="50dp"/>

    <!-- 填充 -->
    <solid
        android:color="@android:color/white"/><!-- 填充的颜色 -->

    <!-- 描边 -->
    <!--android:dashWidth=1dp 虚线的宽度,值为0时是实线  -->
    <!--android:dashGap=2dp 虚线的间隔 -->
    <stroke
        android:width="2dp"
        android:color="@android:color/black"
        android:dashWidth="1dp"
        android:dashGap="2dp"/>

</shape>

app\src\main\res\values\styles
为一系列具有同一样式的View统一分配属性值
例子:

    <style name="MyTextViewStyle" >
        <!-- Customize your theme here. -->
        <item name="android:textSize">18sp</item>
        <item name="android:clickable">true</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:textColor">#400000ff</item>
    </style>

    <style name="MyImageViewStyle" >
        <!-- Customize your theme here. -->
        <item name="android:layout_marginTop">3dp</item>
        <item name="android:visibility">invisible</item>
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">2dp</item>
        <item name="android:src">@drawable/whitebar</item>
        <item name="android:scaleType">fitXY</item>
        <item name="android:color">#ffffff</item>
    </style>

在布局中的使用


        <LinearLayout
            android:id="@+id/line1"
            style="@style/MyLinearLayout"
            >
            <TextView
                android:id="@+id/title1"
                style="@style/MyTextViewStyle"
                android:text="动态" />
            <ImageView
                android:id="@+id/image1"
                style="@style/MyImageViewStyle"/>
        </LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值