给GradientDrawable搞个setPadding
本帖最后由 djwdj 于 2021-5-6 19:55 编辑安卓10才支持setPadding
安卓诞生前就支持getPadding
这就容易解决了
重写一下就好了
上点小学生都懂的代码
package l;
import android.graphics.Rect;
import android.graphics.drawable.GradientDrawable;
/**
* Create User: DJW
* Create Date: 2021/5/6
* Create Time: 19:00
*/
public class gd extends GradientDrawable {
Rect mPadding;
@Override
public boolean getPadding(Rect padding) {
if (mPadding != null) {
padding.set(mPadding);
return true;
} else {
return super.getPadding(padding);
}
}
public void setPadding(int p){
setPadding(p,p,p,p);
}
public void setPadding(int left, int top, int right, int bottom) {//api=29,10.0
if (mPadding == null) {
mPadding = new Rect();
}
mPadding.set(left, top, right, bottom);
invalidateSelf();
}
}
我的用法:
public static Drawable gd2(){
gd gd=new gd();
gd.setColor(0xffaaaaaa);//背景
gd.setStroke(2,0xffecec00);//边框
gd.setCornerRadius(50);//圆角
gd.setPadding(12);//内间距
InsetDrawable id = new InsetDrawable(gd, 10);//外间距
return id;
}
没有主题动态切换,也不想重写
那就上点幼儿园的代码
drawable/a_btn_radius.xml
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="@dimen/bt_ih"
android:insetTop="@dimen/bt_iv"
android:insetRight="@dimen/bt_ih"
android:insetBottom="@dimen/bt_iv">
<shape>
<corners android:radius="@dimen/bt_r" />
<solid android:color="@color/bt_c" />
<stroke android:color="@color/bt_sc"
android:width="@dimen/bt_sw"/>
<padding android:left="@dimen/bt_ph"
android:top="@dimen/bt_pv"
android:right="@dimen/bt_ph"
android:bottom="@dimen/bt_pv" />
</shape>
</inset>
values/a.xml
<resources>
<dimen name="bt_ih" >4dp</dimen>
<dimen name="bt_iv" >6dp</dimen>
<dimen name="bt_pv" >4dp</dimen>
<dimen name="bt_ph" >8dp</dimen>
<dimen name="bt_r" >20dp</dimen>
<dimen name="bt_sw" >1dp</dimen>
<color name="bt_sc" >#ff5555</color>
<color name="bt_c">#ffffff</color>
</resources> 欧 我是幼儿园的不懂
干的好自己写dg
页:
[1]