Android 通过solid来定义不同边框的颜色,可以只定义一个边框的颜色

本文详细介绍了如何在Android中使用XML布局文件为Button设置右边框和底边框的颜色及大小,通过新建buttonstyle.xml文件来实现定制化的UI效果。

以下是设置按钮的右边框和底边框颜色为红色,边框大小为3dp,如下图:

在drawable新建一个 buttonstyle.xml的文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<!-- 连框颜色值 --><item> 
      <shape> 
            <solid android:color="#ff0000" /> 
      </shape> 
</item> 
<!-- 主体背景颜色值 -->
<item android:bottom="3dp" android:right="3dp"> 
     <shape> 
           <solid android:color="#ffffff" />
           
           <padding android:bottom="10dp"
           		android:left="10dp"
           		android:right="10dp"
           		android:top="10dp" />
     </shape>     
</item>
</layer-list>

 

然后在布局文件里面的Button里面设置如下:


<Button
   android:id="@+id/button1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Button1"
   android:background="@drawable/buttonstyle" />


 

<think> 我们需要一个圆环,中间是红色填充,边框颜色(比如蓝色)。这可以通过在shape drawable中同时使用<solid>和<stroke>来实现。 注意:圆环实际上是一个圆形边框,中间用颜色填充。所以我们需要设置填充色(solid)和边框(stroke)。 另外,为了确保是圆形,控件的宽高需要相等(正方形)。 以下是一个示例:中间填充红色,边框为蓝色,宽度为4dp。 创建文件:res/drawable/red_fill_blue_stroke_circle.xml </think> 在 Android 中实现一个中间为红色填充、边框颜色的圆环非常简单,只需在 XML 形状绘制中同时使用 `<solid>` 和 `<stroke>` 元素即可。以下是详细解决方案: ### 解决方案代码 在 `res/drawable` 目录下创建 `red_inner_colored_ring.xml` 文件: ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <!-- 设置内部填充为红色 --> <solid android:color="#FF0000" /> <!-- 设置圆环边框(蓝色边框,宽度8dp) --> <stroke android:width="8dp" android:color="#2196F3" /> <!-- 设置默认尺寸(可选) --> <size android:width="120dp" android:height="120dp" /> </shape> ``` ### 使用示例 在布局文件中应用该 Drawable: ```xml <View android:layout_width="120dp" android:layout_height="120dp" android:background="@drawable/red_inner_colored_ring"/> ``` ### 代码解释 1. **`<solid>` 元素** - `android:color="#FF0000"`:设置圆环内部填充为纯红色 - 这是实现"中间红色"效果的关键元素 2. **`<stroke>` 元素** - `android:width="8dp"`:设置圆环边框宽度为 8dp - `android:color="#2196F3"`:设置圆环边框为蓝色(可替换为任何颜色) - 这些参数控制"边框颜色"的外观 3. **`<size>` 元素(可选)** - 定义 Drawable 的默认尺寸,实际使用时会被布局中的宽高覆盖 ### 进阶用法 #### 1. 根据不同状态改变圆环样式 创建选择器 `res/drawable/selector_ring.xml`: ```xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <shape android:shape="oval"> <solid android:color="#FF5252" /> <!-- 按下时内部变浅红色 --> <stroke android:width="8dp" android:color="#FFC107" /> <!-- 边框变为黄色 --> </shape> </item> <item> <shape android:shape="oval"> <solid android:color="#FF0000" /> <!-- 默认内部红色 --> <stroke android:width="8dp" android:color="#2196F3" /> <!-- 默认边框蓝色 --> </shape> </item> </selector> ``` #### 2. 自定义边框样式 ```xml <shape android:shape="oval"> <solid android:color="#FF0000" /> <stroke android:width="12dp" android:color="#4CAF50" <!-- 边框颜色 --> android:dashWidth="20dp" <!-- 虚线段的长度 --> android:dashGap="10dp" /> <!-- 虚线段的间隔 --> <corners android:radius="100dp" /> <!-- 确保为圆形 --> </shape> ``` #### 3. 使用图层列表实现更复杂效果 ```xml <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 底层:白色背景圆 --> <item> <shape android:shape="oval"> <solid android:color="#FFFFFF" /> <size android:width="130dp" android:height="130dp" /> </shape> </item> <!-- 中层:红色圆 --> <item android:width="120dp" android:height="120dp" android:gravity="center"> <shape android:shape="oval"> <solid android:color="#FF0000" /> </shape> </item> <!-- 顶层:蓝色边框圆环 --> <item> <shape android:shape="oval"> <stroke android:width="8dp" android:color="#2196F3" /> <size android:width="140dp" android:height="140dp" /> </shape> </item> </layer-list> ``` ### 注意事项 1. **确保圆形**:控件必须是正方形(宽高相等),否则会显示为椭圆 2. **边框宽度**:`stroke` 的宽度决定了圆环的厚度 3. **尺寸关系**:边框宽度不会影响内部圆的尺寸,内部圆会自动缩小以适应边框 4. **性能**:对于简单形状,使用 Shape Drawable 比 PNG 图片更节省内存
评论 8
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值