更改 GradientDrawable 的填充

Luc*_*oli 5 android padding android-drawable

我有这个图层列表:

<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/outer_satop_btn">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle" >
            <corners android:radius="5dp" />
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
    <item android:id="@+id/bottom_satop_btn">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle" >
            <corners android:radius="5dp" />
            <solid android:color="@color/satopButtonColor2" />
            <padding
                android:bottom="5dp" />
        </shape>
    </item>
    <item android:id="@+id/top_satop_btn">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle" >
            <solid android:color="@color/satopButtonColor" />
            <corners android:radius="5dp" />
        </shape>
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

我想以编程方式更改 external_satop_btn 的填充底部,但我没有找到任何可以做到这一点的东西:

LayerDrawable bgDrawable = (LayerDrawable)v.getBackground();
final GradientDrawable shape1 = (GradientDrawable)   bgDrawable.findDrawableByLayerId(R.id.outer_satop_btn);
Rect padding = new Rect();
shape1.getPadding(padding);
Run Code Online (Sandbox Code Playgroud)

我能怎么做?谢谢

Nan*_*noc 1

例如,作为可绘制对象,它必须放置在 ImageView 上。

您必须将填充放在绘制它的视图上。

如果使用 LinearLayout 作为 imageViews 父级的代码示例:

ImageView img = new ImageView(this);
img.setDrawable(shape1);
LinearLayout.LayoutParams prms = new LinearLayout.LayoutParams(WIDTH,HEIGHT);
prms.setPadding(LEFT,UP,RIGHT,BOTTOM);
linearLayout.addView(img,prms);
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。