【问题标题】:Square shaped layout border with round inside edges具有圆形内边缘的方形布局边框
【发布时间】:2011-09-24 17:46:29
【问题描述】:

我正在尝试创建一个外边为方形,内边为圆角的布局边框。我收集到我需要创建一个由两种形状组成的 .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

<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" 

但是当它们中的一个或两个都被添加到 item-list drawable 中时:

composite_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"

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

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

【问题讨论】:

  • 感谢分享!

标签: android list border layer


【解决方案1】:

Shape 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>

请注意,我按照 layer-list 文档中的说明更改了 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
并且您希望它从外部平方

【讨论】:

    【解决方案2】:

    试试这个就够用了:

    solid背景色

    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>
    

    【讨论】:

    • 如果我想要一个填充 bw 边框和实心怎么办?
    【解决方案3】:

    创建一个像round_background.xml这样的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>
    

    在设置为背景的布局中

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

    【讨论】:

      【解决方案4】:

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

      注意 cmets 和引号! =]

      【讨论】:

      • 谢谢 - 仍在寻找答案。
      猜你喜欢
      • 2021-05-09
      • 2019-03-25
      • 1970-01-01
      • 1970-01-01
      • 2018-11-14
      • 1970-01-01
      • 2016-05-26
      • 1970-01-01
      • 2022-09-28
      相关资源
      最近更新 更多