【问题标题】:How to add padding for vertical line shape in android如何在android中为垂直线形状添加填充
【发布时间】:2016-09-26 13:00:33
【问题描述】:

我的视图有一个图层列表背景

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape>
        <solid android:color="@color/white"/>
        <corners android:radius="3dp"/>
        <stroke
            android:width="1dp"
            android:color="@color/warm_grey"/>
    </shape>
</item>
<item
    android:left="10dp"
    android:right="10dp">
    <shape
        android:shape="line">
        <stroke
            android:width="1.5dp"
            android:color="@color/blue"/>
    </shape>
</item>
<item
    android:bottom="10dp"
    android:top="10dp">
    <rotate
        android:fromDegrees="90"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="-90">
        <shape
            android:shape="line">
            <stroke android:width="1.5dp" android:color="@color/blue"/>
        </shape>
    </rotate>
</item>
</layer-list>

在这里,我尝试使用 top、bottom、left 和 right 属性为上面的行设置填充,它适用于水平线,但不适用于垂直线。

如何为垂直线形状添加垂直填充?

【问题讨论】:

  • 试试,在形状块里面,加padding,比如stroke/corners/solid

标签: android android-drawable android-shape


【解决方案1】:

如果是垂直线,请尝试左右。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="@color/light_blue"/>
            <corners android:radius="3dp"/>
            <stroke
                android:width="1dp"
                android:color="@color/warm_grey"/>
        </shape>
    </item>
    <item
        android:left="10dp"
        android:right="10dp">
        <shape
            android:shape="line">
            <stroke
                android:width="1.5dp"
                android:color="@color/blue"/>
        </shape>
    </item>
    <item
        android:left="10dp"
        android:right="10dp"
        >
        <rotate
            android:fromDegrees="90"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toDegrees="-90">
            <shape
                android:shape="line">
                <stroke android:width="1.5dp" android:color="@color/blue"/>
            </shape>
        </rotate>
    </item>
</layer-list>

这对我有用。

【讨论】:

  • 这确实有效,但如果视图不是正方形,则在旋转线之前应用这些填充,因此它似乎不是一个完美的解决方案。
【解决方案2】:

这里是创建在线按钮或任何视图形状的链接链接:http://angrytools.com/android/button/

代码如下:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="14dp" />
    <gradient
        android:angle="45"
        android:centerColor="#7995A8"
        android:centerX="35%"
        android:endColor="#000000"
        android:startColor="#E8E8E8"
        android:type="linear" />
    <padding
        android:bottom="0dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp" />
    <size
        android:width="270dp"
        android:height="60dp" />
    <stroke
        android:width="3dp"
        android:color="#878787" />
</shape>

【讨论】:

    猜你喜欢
    • 2015-04-24
    • 2017-03-23
    • 1970-01-01
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 2019-09-18
    相关资源
    最近更新 更多