Android 填充不会影响 XML 布局中的 <shape>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1283085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:53:56  来源:igfitidea点击:

Padding doesn't affect <shape> in an XML Layout

androiduser-interface

提问by Lyubomyr Dutko

I am trying to set padding in a <shape>declared within an XML file/layout. But whatever I set, nothing changes related to padding. If I modify any other properties, I see the effects on the UI. But it doesn't work with padding. Could you please advise on the possible reasons for which this might occur?

我正在尝试<shape>在 XML 文件/布局中声明的内容中设置填充。但是无论我设置什么,都没有与填充相关的变化。如果我修改任何其他属性,我会看到对 UI 的影响。但它不适用于填充。您能否就可能发生这种情况的可能原因提出建议?

Here is the XML Shape I am trying to style:

这是我尝试设置样式的 XML 形状:

 <shape xmlns:android="http://schemas.android.com/apk/res/android" 
      android:shape="rectangle">

    <stroke android:width="1dp" 
            android:color="#ffffff" 
            android:dashWidth="2dp"/>

    <solid android:color="@color/button_white_cover"/>

    <corners android:radius="11dp"/>

    <padding android:left="1dp" 
             android:top="20dp"
             android:right="20dp" 
             android:bottom="2dp"/>
 </shape>

回答by Lyubomyr Dutko

I have finally resolved my problem with padding.

我终于解决了我的填充问题。

So padding here will have no effect on the shape. Instead of this, you will have to apply padding in other drawable that will use it. So, I have a drawable that uses the shape and there I do following:

所以这里的填充对形状没有影响。取而代之的是,您必须在将使用它的其他可绘制对象中应用填充。所以,我有一个使用该形状的可绘制对象,并在那里执行以下操作:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable="@drawable/shape_below"/>
   <item android:top="10px" android:right="10px" android:drawable="@drawable/shape_cover"/>
</layer-list>

So, as you can see in the second <item>-element I set padding (top and right). And after this everything works.

因此,正如您在第二个<item>元素中看到的那样,我设置了填充(顶部和右侧)。在此之后一切正常。

Thanks.

谢谢。

回答by TheIT

As a couple of comments have alluded to, the best approach is to wrap the <shape>in an <item>element and set the padding there instead. There are however other options available which are detailed in the link below. eg

正如一些评论所提到的,最好的方法是将 包装<shape>在一个<item>元素中并在那里设置填充。但是,还有其他可用选项,详情请参见下面的链接。例如

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:left="8dp"
        android:right="8dp"
        android:top="8dp"
        android:bottom="8dp">

        <shape android:shape="rectangle">
            ...
        </shape>
    </item>
</layer-list>

Depending on your needs, instead of using a layer-list, you could use any of the following drawable types:

根据您的需要,您可以使用以下任何一种可绘制类型,而不是使用图层列表:

  • layer-list
  • selector
  • level-list
  • transition
  • 层列表
  • 选择器
  • 级别列表
  • 过渡

For more information about the available drawable types see this android documentation

有关可用的可绘制类型的更多信息,请参阅此 android 文档

Sources:

资料来源:

回答by Yahel

The answer by Lyobomyr works, but here is the same solution applied but without the overhead of creating a new drawable, hence minimizing the file clutter :

Lyobomyr 的答案有效,但这里应用了相同的解决方案,但没有创建新可绘制对象的开销,从而最大限度地减少文件混乱:

https://stackoverflow.com/a/3588976/578746

https://stackoverflow.com/a/3588976/578746

Copy/Paste of the anwser by anon on the SO answer above :

anon 在上面的 SO 答案中复制/粘贴 anwser :

<item android:left="6dp"
      android:right="6dp">

    <shape android:shape="rectangle">
        <gradient android:startColor="#ccd0d3"
                  android:centerColor="#b6babd"
                  android:endColor="#ccd0d3"
                  android:height="1px"
                  android:angle="0"/>
    </shape>
</item>

回答by Witoldio

You can try insets:

您可以尝试插入:

<?xml version="1.0" encoding="UTF-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetBottom="2dp"
    android:insetLeft="1dp"
    android:insetRight="20dp"
    android:insetTop="20dp">

    <shape android:shape="rectangle">

        <stroke
            android:width="1dp"
            android:color="#ffffff"
            android:dashWidth="2dp" />

        <solid android:color="@color/button_white_cover" />

        <corners android:radius="11dp" />
    </shape>

</inset>

回答by riverlet

I solved this problem like this:

我是这样解决这个问题的:

<shape android:shape="oval"  >
    <stroke android:width="4dp" android:color="@android:color/transparent" />
    <solid android:color="@color/colorAccent" />
</shape>

just added a transparent stroke to it.

刚刚添加了一个透明的笔触。