布局添加背景边框样式工具类

1.布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <android.support.v7.widget.LinearLayoutCompat
        android:id="@+id/linear_layout"
        android:layout_width="match_parent"
        android:layout_margin="20dp"
        android:layout_height="300dp"
        android:gravity="center">

        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/tv_test"
            android:layout_width="100dp"
            android:layout_height="100dp" />
    </android.support.v7.widget.LinearLayoutCompat>
</android.support.v7.widget.LinearLayoutCompat>

2.效果

3.代码

linearLayout.setBackground(ShapeUtil.drawViewShape(this,50f,20f,R.color.green,R.color.black));
tvTest.setBackground(ShapeUtil.drawViewShape(this,50f,2f,R.color.white,R.color.red));

4.工具类

public class ShapeUtil {

    /**
     * @param context
     * @param roundRadius   圆角大小
     * @param strokeWidth   线宽度
     * @param fillColorId   填充色
     * @param strokeColorId 线颜色
     * @return
     */
    public static GradientDrawable drawViewShape(Context context, Float roundRadius, Float strokeWidth, int fillColorId, int strokeColorId) {
        int strokeColor = 0;
        if (strokeColorId > 0) {
            //边线颜色
            strokeColor =ContextCompat.getColor(context, strokeColorId);
        }
        //创建drawable
        GradientDrawable gd = new GradientDrawable();
        //内部填充颜色
        if (fillColorId > 0) {
            gd.setColor(ContextCompat.getColor(context, fillColorId));
        }
        gd.setCornerRadius(DisplayUtil.dipToPix(context, roundRadius));
        if (strokeWidth > 0 && strokeColor != 0) {
            gd.setStroke((int) DisplayUtil.dipToPix(context, strokeWidth), strokeColor);
        }
        return gd;
    }
}


public class DisplayUtil {
    /**
     * 根据dip值转化成px值
     *
     * @param context
     * @param dip
     * @return
     */
    public static float dipToPix(Context context, float dip) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, context.getResources().getDisplayMetrics());
    }
}
class ShapeUtil {

    companion object {
        fun drawViewShape(context: Context, roundRadius: Float, strokeWidth: Float,
                            fillColorId: Int, strokeColorId: Int): GradientDrawable {
            var strokeColor = 0
            if (strokeColorId > 0) {
                strokeColor = ContextCompat.getColor(context, strokeColorId)
            }
            val gd = GradientDrawable()
            if (fillColorId > 0) {
                gd.setColor(ContextCompat.getColor(context, fillColorId))
            }
            gd.cornerRadius = DisplayUtil.dipToPx(context, roundRadius)
            if (strokeWidth > 0 && strokeColor != 0) {
                gd.setStroke((DisplayUtil.dipToPx(context, strokeWidth)).toInt(), strokeColor)
            }
            return gd
        }
    }
}


class DisplayUtil {

    companion object {
        fun dipToPx(context: Context, dip: Float): Float {
            return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, context.resources.displayMetrics)
        }
    }
}

这样就不用写烦人的Drawable了!告辞!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值