shape

本文深入解析了Android应用中形状控件的XML语法与配置方法,包括矩形、椭圆、线、环形等基本形状,以及它们的属性如圆角、颜色渐变、描边等高级特性。通过实例展示了如何在TextView和ImageView中应用这些形状,以增强用户界面的美观性和功能性。

参考: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"

    android:shape="rectangle">

    <!--shape命名空间  类型-->

    <!--android:shape="rectangle"   表示矩形-->
    <!--android:shape="oval"        表示椭圆-->
    <!--android:shape="line"        表示线-->

    <!--android:shape="ring"        表示环形-->
        <!--android:innerRadius         尺寸,内环的半径。-->
        <!--android:thickness           尺寸,环的厚度-->
        <!--android:innerRadiusRatio    浮点型,以环的宽度比率来表示内环的半径,-->
        <!--例如,如果android:innerRadiusRatio,表示内环半径等于环的宽度除以5,这个值是可以被覆盖的,默认为9.-->
        <!--android:thicknessRatio      浮点型,以环的宽度比率来表示环的厚度,例如,如果android:thicknessRatio="2",-->
        <!--那么环的厚度就等于环的宽度除以2。这个值是可以被android:thickness覆盖的,默认值是3.-->
        <!--android:useLevel            boolean值,如果当做是LevelListDrawable使用时值为true,否则为false.-->


    <!--shape 的子节点-->

        <!--corners     圆角-->
        <!--gradient    对应颜色渐变-->
        <!--padding     定义内容离边界的距离-->
        <!--size        大小-->
        <!--solid       填充-->
        <!--stroke      描边-->


    <!--corners的命名空间-->
        <!--同时设置五个属性,则Radius属性无效-->
        <!--android:radius="20dp"           圆角的半径(4个角) -->
        <!--android:bottomLeftRadius="8dp"  左下圆角的半径-->
        <!--android:bottomRightRadius="5dp" 右下圆角的半径-->
        <!--android:topLeftRadius="6dp"     左上圆角的半径-->
        <!--android:topRightRadius="5dp"    右上圆角的半径-->

    <corners
        android:bottomLeftRadius="8dp"
        android:bottomRightRadius="5dp"
        android:topLeftRadius="6dp"
        android:topRightRadius="5dp" />



    <!--padding android:left="5dp"与padding_left属性效果相同 padding没什么用处-->
    <!--<padding-->
        <!--android:bottom="5dp"-->
        <!--android:left="5dp"-->
        <!--android:right="5dp"-->
        <!--android:top="5dp" />-->


    <size
        android:width="100dp"
        android:height="50dp" />

    <!--android:color="#ff00ff"             填充的颜色(如果与渐变色一起使用,会被覆盖)-->
    <solid android:color="#ff00ff" />


    <!--dashWidth和dashGap属性,只要其中一个设置为0dp,则边框为实现边框-->
    <!--android:width="2dp"                 线宽-->
    <!--android:color="#000000"             线的颜色-->
    <!--android:dashGap="3dp"               虚线的长度-->
    <!--android:dashWidth="10dp"            虚线间隔的长度-->
    <stroke
        android:width="2dp"
        android:color="#000000"
        android:dashGap="3dp"
        android:dashWidth="10dp" />



    <!--gradient android:type="linear"-->
    <!--angle属性确实只对线性渐变有效-->

    <!--<gradient-->
    <!--android:angle="45"-->
    <!--android:centerColor="#00ff00"-->
    <!--android:endColor="#0000ff"-->
    <!--android:startColor="#ff0000"-->
    <!--android:type="linear" />-->


    <!--centerX、centerY两个属性用于设置渐变的中心点位置,仅当渐变类型为放射渐变时有效。
        默认值是0.5,有效值是0.0~1.0,超出该范围后会看不出渐变效果。-->

    <!--<gradient-->
        <!--android:centerColor="#00ff00"-->
        <!--android:centerX="0.5"-->
        <!--android:centerY="0.5"-->
        <!--android:endColor="#0000ff"-->
        <!--android:startColor="#ff0000"-->
        <!--android:type="sweep" />-->

    <!--在构造放射性渐变时,要加上android:gradientRadius属性(渐变半径),即必须指定渐变半径的大小才会起作用-->
    <!--<gradient-->
        <!--android:type="radial"-->
        <!--android:startColor="#ff0000"-->
        <!--android:centerColor="#00ff00"-->
        <!--android:endColor="#0000ff"-->
        <!--android:gradientRadius="40"/>-->

</shape>

在控件中使用

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="hello world"
    android:background="@drawable/shape_rect"/>
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/shape_rect"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值