Shape形状

本文详细介绍了如何在Android中使用ShapeDrawable来定义各种图形样式,包括矩形、椭圆及环形,并展示了如何通过XML文件设置渐变色、边框、圆角等属性,最后给出了在TextView中应用这些样式的实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Shape Drawable
This is a generic shape defined in XML.
EXAMPLE:
XML file saved at res/drawable/gradient_box.xml: 在Res文件夹下,drawable 下。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">   //矩形   oval :椭圆形    ring : 环形。
    <gradient
        android:startColor="#FFFF0000"
        android:endColor="#80FF00FF"
        android:angle="45"/>
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
    <corners android:radius="8dp" />  //圆角 的半径  
</shape>

This layout XML applies the shape drawable to a View:

<TextView
    android:background="@drawable/gradient_box"  //background 的属性可以用 定义好的 shape
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" />

shape形状的属性;


   <corners android:radius="5dp"/>  //  圆角 半径是5dp;  
   <gradient android:startColor=""/>  // 颜色的渐变。
    <solid android:color="" />   //表示纯色。
    <stroke  android:width="" />  //表示边框。  边框的宽度。
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/gradient_box" android:state_pressed="true"/>
 <!-- pressed -->

    <item android:drawable="@android:color/transparent"/>  //状态选择器中,默认的颜色是 透明色。
 <!-- default -->

</selector>   

在layout布局中的应用:

<TextView
            style="@style/ContentStyle"
            android:layout_width="match_parent"
            android:text="重新进入设置向导" 
            android:onClick="Restart"
            android:background="@drawable/shape_selector"  //textview 中背景。
            android:clickable="true"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值