Android 代码创建 Shape 属性 (solid, corner, stroke, color)

UI设计图都是带圆角的,简单写一个 Shape 属性搞定。但是需要每个 Shape 属性的背景颜色都不一样,那就需要在代码中直接创建 Shape 属性。

我个人是很不喜欢圆角的设计,现在应用图标也改成了圆角,点击应用图标我都有负担啊。

<?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>
    // prepare
    int strokeWidth = 5; // 3px not dp
    int roundRadius = 15; // 8px 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);

参考资料
How to create shape with solid, corner, stroke in Java Code
Android代码设置Shape,corners,Gradient

    原文作者:孙鹏SIMPLE
    原文地址: https://www.jianshu.com/p/bc89f7e32d43
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞