android - 如何从代码中更改 `solid color`?

标签 android android-layout android-xml

我在 xml 中定义了一个 Shape。现在我需要从代码中更改一个 solid color,请问如何操作?

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" android:padding="10dp">
    <solid android:color="#000000"/>
    <corners
        android:bottomRightRadius="8dp"
        android:bottomLeftRadius="8dp"
        android:topLeftRadius="0dp"
        android:topRightRadius="0dp"/>
</shape>

最佳答案

我在我的代码中使用它

GradientDrawable myGrad = (GradientDrawable)rectangle.getBackground();
myGrad.setColor(Color.BLACK);

希望这有帮助:)

编辑:GradientDrawable 而不是 ShapeDrawable

关于android - 如何从代码中更改 `solid color`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16775891/

相关文章:

java - 无法粗体或突出显示 Android 自定义日历的事件日期

android - 我的 android 动画时间有什么问题?

android - 将四个按钮置于屏幕四分之一的中心

android - 如何在 Android 中将解析的 Xml 数据显示到 ListView 中

android - 在哪里可以阅读有关 id 属性格式的信息?

android - OpenCv4Android 和 C++ 数据类型之间的混淆

android - 如何获取 youtube 网址并在我的应用中播放?

android - SQLite 在 Samsung Galaxy GT-I9000 中非常慢

android - 非法参数异常 : LinearLayoutManager is already attached to a RecyclerView

java - 将 View 添加到抽屉导航中的抽屉布局