android - 在 LinearLayout 中更改圆圈的颜色

标签 android android-linearlayout drawable

我有一个像这样的 LinearLayout:

 <LinearLayout
                android:background="@drawable/circle"
                android:layout_gravity="center_horizontal"
                android:orientation="vertical"
                android:id="@+id/powerCircle"
                android:layout_width="20dp"
                android:layout_height="20sp" />

然后我有一个彩色圆圈的背景,像这样:

圆.xml

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

    <solid
        android:color="#666666"/>

    <size
        android:width="20dp"
        android:height="20dp"/>
</shape>

在我的自定义适配器中,我想根据状态变量更改圆圈的背景颜色。

如何获取对圆圈背景的引用?

最佳答案

也许,这样的事情应该可行:

GradientDrawable background = (GradientDrawable) linearView.getBackground();
background.setColor(getResources().getColor(R.color.some_color));

关于android - 在 LinearLayout 中更改圆圈的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29437289/

相关文章:

android - LinearLayout 扩展元素

较低API级别的Android Drawable Shape Ring

android - 通过布局更改 xml 可绘制文件中形状的颜色

android - Java 到安卓?

java - 在 Android 上使用哪个 SVM 库?

android - 在 Android 2.2 中以编程方式启动 'Add Account' Activity

android - 是否可以在 Google Play 上更改 Android 应用程序的包名称?

android - LinearLayout * 元素大小

Android 'Couldn' t expand remoteViews for ...' 以编程方式创建布局时

android - 在 Drawable 中将图像源设置为 Bitmap