gpt4 book ai didi

android - 具有圆形内边缘的方形布局边框

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:02:57 33 4
gpt4 key购买 nike

我正在尝试创建一个布局边框,其角在外面是方形的,在里面是圆形的。我发现我需要创建一个由两种形状组成的 .xml 可绘制对象定义:一种具有笔划宽度和圆角半径,另一种仅具有笔划宽度:

可绘制对象

round_border.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="4dp" android:color="#FF000000" />
<padding android:left="7dp" android:top="7dp"
android:right="7dp" android:bottom="7dp" />
<corners android:radius="4dp" />
<solid android:color="#FFC0C0C0" />
</shape>

方形边框.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="2dp" android:color="#FF000000" />
<solid android:color="#FFC0C0C0" />
</shape>

当像这样单独应用时,这些中的每一个都作为边框独立工作:

android:background="@drawable/round_border" 

但是当它们中的一个或两个都像这样被添加到一个项目列表可绘制对象中时:

复合边框.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<layer-list>
<item android:drawable="@drawable/round_border"/>
<!-- <item android:drawable="@drawable/square_border"/> -->
</layer-list>
</shape>

和:

android:background="@drawable/composite_border"

布局的背景是完全黑色的,而不仅仅是黑色边框。

有谁知道如何使层列表适用于此任务?

最佳答案

来自 Shape Drawable Doc你可以看到 shape 里面没有 layer-list 所以你应该像这样定义你的 composite_border.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/square_border"/>
<item android:drawable="@drawable/round_border"/>
</layer-list>

请注意,我按照层列表文档中的说明更改了层列表中项目的顺序
列表中的每个 drawable 都按列表的顺序绘制——列表中的最后一个 drawable 绘制在顶部
并且您希望它从外部平方

关于android - 具有圆形内边缘的方形布局边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6514114/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com