简体   繁体   English

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

[英]Square shaped layout border with round inside edges

I'm attempting to create a layout border with corners that are square on the outside and round on the inside.我正在尝试创建一个布局边框,其角在外面是方形的,在里面是圆形的。 I've gathered that I need to create an.xml drawable definition composed of two shapes: one with a stroke width and corner radius and another with a stroke width only:我收集到我需要创建一个由两种形状组成的.xml 可绘制定义:一个具有笔划宽度和角半径,另一个仅具有笔划宽度:

The drawables可绘制对象

round_border.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> 

square_border.xml square_border.xml

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

Each of these works independantly as a border when appliedby itself like so:当单独应用时,这些中的每一个都独立地作为边界工作,如下所示:

android:background="@drawable/round_border" 

but when they either or both are added to an item-list drawable like so:但是当它们中的一个或两个都被添加到可绘制的项目列表中时,如下所示:

composite_border.xml复合边框.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> 

and:和:

android:background="@drawable/composite_border"

The layout's background is completely black instead of just a black border.布局的背景是完全黑色的,而不仅仅是黑色边框。

Does anyone know how to make the layer list work for this task?有谁知道如何使图层列表适用于此任务?

From Shape Drawable Doc you can see that shape can't have layer-list inside so you should define your composite_border.xml like thisShape Drawable Doc中,您可以看到该形状内部没有图层列表,因此您应该像这样定义您的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>

note that I changed the order of your items inside the layer-list as stated in the documentation of layer-list请注意,我更改了图层列表中项目的顺序,如图层列表文档中所述
Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top
and you want it to be squared from outside你想让它从外面平方

Create a xml file like round_background.xml:创建一个 xml 文件,如 round_background.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
   <solid 
       android:color="#CCCC33"/>
   <size 
       android:width="35dp"
        android:height="35dp"/>
</shape>

In the layout set as background在设置为背景的布局中

<LinearLayout
    android:id="@+id/layout_wellbeing"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="center"
    android:background="@drawable/rounded_corner_leuvan"
    android:orientation="horizontal" >
</LinearLayout>

square_border.xml square_border.xml

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

composite_border.xml复合边框.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>

watch out the comments and the quotation marks!注意评论和引号! =] =]

Try this will work fine enough:试试这个就足够了:

solid is background color solid背景色

stroke is border stroke边界

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
   <solid 
       android:color="@color/white"/>
   <stroke android:width="1dp" android:color="#ffaaaaaa" />
   <size 
       android:width="15dp"
        android:height="15dp"/>
</shape>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM