zoukankan      html  css  js  c++  java
  • Android自己定义矩形及selector、shape的使用

    【声明】转载请注明出处。此文出自指尖飞落的博客:http://blog.csdn.net/huntersnail

    ——每天写一篇博客。每天做一点技术积累!


    Android自己定义矩形及selector、shape的使用

    因为项目开发须要,曾经尽管用过selector、shape可是都没有好好去研究过,仅仅知道用。不知道它们的属性详细有哪些作用。

    尽管网上一查就都知道了,感觉还是要自己去弄懂一下。

    以下咱们一起去研究下:


    一、xml布局文件

    /測试Demo/res/layout/check_box.xml

    <?

    xml version="1.0" encoding="utf-8"?

    > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/order_meth" style="@style/style_padding" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:background="@drawable/selector_mine" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="送达方式" android:textSize="18sp" /> <RadioGroup android:id="@+id/type" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal" > <RadioButton android:id="@+id/rb_one" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/sku_rd" android:layout_marginLeft="20dp" android:gravity="center" android:checked="true" android:padding="10dp" android:text="一小时达" /> <RadioButton android:id="@+id/rb_two" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/sku_rd" android:layout_marginLeft="10dp" android:gravity="center" android:padding="10dp" android:text="预定送达" /> </RadioGroup> </LinearLayout> </LinearLayout>


    二、点击时背景颜色处理

    1、style样式

    <resources xmlns:android="http://schemas.android.com/apk/res/android">
        <style name="sku_rd">
            <item name="android:textColor">@color/selector_text</item>
            <item name="android:background">@drawable/check_boxs</item>
            <item name="android:button">@null</item><!--去掉RadioButton的那个圆形-->
        </style>
    </resources>


    2、点击RadioButton边框背景颜色变换的关键xml

    属性解读

    “true”表示按下状态使用(比如button按下)。“false”表示非按下状态使用。


                android:state_focused="true/false"

    “true”表示聚焦状态使用(比如使用滚动球/D-pad聚焦Button);“false”表示非聚焦状态使用。
                android:state_selected="true/false"

    “true”表示选中状态使用(比如Tab打开);“false”表示非选中状态使用。
                android:state_active="true/false"

    “true”表示可勾选状态时使用;“false”表示非可勾选状态使用。

    (仅仅对能切换可勾选—非可勾选的构件实用。)
                android:state_checkable="true/false"

     “true”表示勾选状态使用。“false”表示非勾选状态使用。
                android:state_checked="true/false"

    true”表示勾选状态使用;“false”表示非勾选状态使用。


                android:state_enabled="true/false"

    “true”表示可用状态使用(能接收触摸/点击事件)。“false”表示不可用状态使用。


                android:state_window_focused="true/false"

    “true”表示应用程序窗体有焦点时使用(应用程序在前台);“false”表示无焦点时使用(比如Notification栏拉下或对话框显示)。


    /測试Demo/res/drawable/check_boxs.xml

    <?

    xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_checked="true" android:drawable="@drawable/shape_sku"></item> <item android:state_pressed="true" android:drawable="@drawable/shape_sku"></item> <item android:state_selected="true" android:drawable="@drawable/shape_sku"></item> <item android:drawable="@drawable/shape_sku_normal"></item> </selector>


    3、文字颜色的变化

    /測试Demo/res/color/selector_text.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- 文字选中时的背景-->
        <item android:state_focused="true" android:color="#DB251F"/>
        <!-- 文字单击时的背景 -->
        <item android:state_pressed="true" android:color="#DB251F"/>
        <item android:state_checked="true" android:color="#DB251F"/>
        <item android:state_selected="true" android:color="#DB251F"/>
        <!-- 文字默认的背景 -->
        <item android:color="#969696"/>
    </selector>


    三、以下是自己定义的两个矩形

    属性解读

    corners:圆角

    android:radius为角的弧度,值越大角越圆。
    我们还能够把四个角设定成不同的角度。同一时候设置五个属性。则Radius属性无效
    android:Radius="20dp"                          设置四个角的半径
    android:topLeftRadius="20dp"              设置左上角的半径 
    android:topRightRadius="20dp"           设置右上角的半径 
    android:bottomLeftRadius="20dp"       设置右下角的半径 
    android:bottomRightRadius="20dp"    设置左下角的半径


    stroke:描边
    android:width="2dp" 描边的宽度

    android:color 描边的颜色。


    我们还能够把描边弄成虚线的形式,设置方式为:
    android:dashWidth="5dp" 
    android:dashGap="3dp"
    当中android:dashWidth表示'-'这样一个横线的宽度,android:dashGap表示之间隔开的距离。


    solid:填充
    android:color指定填充的颜色


    /測试Demo/res/drawable/shape_sku_normal.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <stroke android:width="1dp" android:color="#C8C8C8"/>
        <corners android:radius="3dp"/>
        <solid android:color="@android:color/white"/>
    </shape>
    /測试Demo/res/drawable/shape_sku.xml
    <?

    xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <stroke android:width="1dp" android:color="#DB251F"/> <corners android:radius="3dp"/> <solid android:color="@android:color/white"/> </shape>


    四、效果图


    五、源代码地址点击打开链接


    ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆转载请注明出处☞指尖飞落的博客☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆

  • 相关阅读:
    两种无限递归菜单
    python导出项目所有依赖库,在新环境中安装所有依赖库
    python3.6中安装Crypto模块
    在Django中settings文件内,配置MySQL和Redis
    Django继承AbstractUser新建User Model时出现fields.E304错误
    django_orm操作MySQL数据库,批量插入数据
    在用脚手架搭建vue项目时,运行vue报错
    django命令行运行失败
    redis pipline 操作
    tcpdump 与 MySQL sniffer 【转】
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/7084728.html
Copyright © 2011-2022 走看看