20

以下のxmlファイルで形状を定義していますが、プログラムで単色に変更したいと考えています。

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

    <solid android:color="#DFDFE0" />

    <corners
        android:bottomLeftRadius="8dp"
        android:bottomRightRadius="8dp"
        android:topLeftRadius="8dp"
        android:topRightRadius="8dp" />

    <stroke
        android:width="3dp"
        android:color="#2E3135" />

</shape>

ShapeDrawable を拡張し、onDraw メソッドを実装するクラスが必要だと思います。誰でも方法を知っていますか?

4

2 に答える 2

28

最後に、私はそれを解決しました!

// prepare
int strokeWidth = 5; // 5px not dp
int roundRadius = 15; // 15px not dp
int strokeColor = Color.parseColor("#2E3135");
int fillColor = Color.parseColor("#DFDFE0");

GradientDrawable gd = new GradientDrawable();
gd.setColor(fillColor);
gd.setCornerRadius(roundRadius);
gd.setStroke(strokeWidth, strokeColor);
于 2013-07-16T07:08:26.630 に答える
3

形を整えて、内側の形にパディングを追加したいのですが...

このような:

<?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="#2E3135" />
            <corners
                android:bottomLeftRadius="8dp"
                android:bottomRightRadius="8dp"
                android:topLeftRadius="8dp"
                android:topRightRadius="8dp" />
        </shape>
    </item>
    <item
        android:bottom="3dp"
        android:left="3dp"
        android:right="3dp"
        android:top="3dp">
        <shape android:shape="rectangle">
            <solid android:color="#DFDFE0" />
            <corners
                android:bottomLeftRadius="5dp"
                android:bottomRightRadius="5dp"
                android:topLeftRadius="5dp"
                android:topRightRadius="5dp" />
        </shape>
    </item>
</layer-list>

更新: 見栄えを良くするために、内側の形状の角を小さくする必要があることがわかりました...異なる比率については、どの内側半径、外側半径、およびストロークサイズ(パディング)がソリューションに最適かをテストする必要があります

于 2016-01-25T09:42:10.773 に答える