Android 代碼創(chuàng)建 Shape 屬性 (solid, corner, stroke, color)

UI設(shè)計(jì)圖都是帶圓角的,簡單寫一個(gè) Shape 屬性搞定。但是需要每個(gè) Shape 屬性的背景顏色都不一樣,那就需要在代碼中直接創(chuàng)建 Shape 屬性。

我個(gè)人是很不喜歡圓角的設(shè)計(jì),現(xiàn)在應(yīng)用圖標(biāo)也改成了圓角,點(diǎn)擊應(yīng)用圖標(biāo)我都有負(fù)擔(dān)啊。

<?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代碼設(shè)置Shape,corners,Gradient

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容