5

需要在View使用Shapes中绘制“X” ,但 X 的边缘必须锚定在视图的左侧、顶部、右侧和底部。

像这样的东西:

示例图像

4

3 回答 3

13

通过可绘制的xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item>
    <rotate
        android:fromDegrees="135"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="135">
        <shape android:shape="line">
            <stroke android:width="1dp" android:color="@color/social_grey" />
        </shape>
    </rotate>
</item>
<item>
    <rotate
        android:fromDegrees="45"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="45">
        <shape android:shape="line">
            <stroke android:width="1dp" android:color="@color/social_grey" />
        </shape>
    </rotate>
</item>


</layer-list>

带填充:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item
   android:left="4dp"
    android:right="4dp"
    >
    <rotate
        android:fromDegrees="135"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="135">
        <shape android:shape="line">
            <stroke android:width="1dp" android:color="@color/social_grey" />
        </shape>
    </rotate>
</item>
<item
    android:left="4dp"
    android:right="4dp"
    >
    <rotate
        android:fromDegrees="45"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="45">
        <shape android:shape="line">
            <stroke android:width="1dp" android:color="@color/social_grey" />
        </shape>
    </rotate>
</item>
</layer-list>
于 2017-11-08T13:02:41.580 回答
10

通过创建自定义 View 并覆盖 onDraw 方法会容易得多。IE。

public class XView extends View {

    @Override
    public void onDraw(Canvas canvas) {
        float width = getMeasuredWidth();
        float height = getMeasuredHeight();
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        canvas.drawLine(0,0,width,height,paint);
        canvas.drawLine(width,0,0,height,paint);
    }
}
于 2012-06-18T16:26:38.460 回答
0

我遇到了类似的情况,但我的盒子有预定义的高度和宽度,只使用 XML

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

        <shape android:shape="rectangle">

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

        </shape>

    </item>

    <item android:top="2dp"
        android:left="2dp"
        android:right="2dp"
        android:bottom="2dp">

        <shape android:shape="rectangle">
            <solid android:color="@color/white" />
        </shape>

    </item>

    <item android:top="14sp"
        android:left="6dp"
        android:right="6dp"
        android:bottom="14sp">

        <rotate
            android:fromDegrees="45"
            android:toDegrees="45"
            android:pivotX="50%"
            android:pivotY="50%" >

            <shape android:shape="rectangle">
                <solid android:color="@color/status_expired_color" />
            </shape>

        </rotate>

    </item>

    <item android:top="6dp"
        android:left="14sp"
        android:right="14sp"
        android:bottom="6dp">

        <rotate
            android:fromDegrees="45"
            android:toDegrees="45"
            android:pivotX="50%"
            android:pivotY="50%" >

            <shape android:shape="rectangle">
                <solid android:color="@color/status_expired_color" />
            </shape>

        </rotate>

    </item>

</layer-list>

@color/status_expired_color = #E9510E
@color/white = #ffffff  

输出:
在此处输入图像描述

于 2015-07-22T06:57:13.667 回答