【问题标题】:Reusing a border resource with multiple background colors重用具有多种背景颜色的边框资源
【发布时间】:2015-03-03 05:08:34
【问题描述】:

我的 /res/drawable 文件夹中目前有以下 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<solid
    android:color="@color/transparent_white" />

<stroke
    android:width="1dip"
    android:color="@color/light_gray" />
</shape>

有时我希望背景为白色、灰色、蓝色等,具体取决于我要设置边框的项目。有没有办法在不创建n个xml文件的情况下做到这一点,唯一的区别是solid属性的颜色?

【问题讨论】:

    标签: android-layout android-shape


    【解决方案1】:

    您可以通过在运行时动态声明形状并更改颜色来做到这一点。

    ShapeDrawable shapeDrawable= new ShapeDrawable();
    shapeDrawable.setShape(new RectShape());
    shapeDrawable.getPaint().setColor(<your color>);
    
    ((TextView) row.findViewById(<your viewid>)).setBackgroundDrawable(shapeDrawable);
    

    【讨论】:

    • 我明白你在说什么,但我正在寻找使用XML 的解决方案,而不是Java,因为它非常简单。我想要 XML 解决方案,因为它使编码变得容易。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    相关资源
    最近更新 更多