gpt4 book ai didi

android - 如何在android中更改可绘制形状的颜色

转载 作者:IT老高 更新时间:2023-10-28 22:02:23 25 4
gpt4 key购买 nike

我正在开发小型 android 应用程序,其中我将可绘制资源设置为线性布局的背景。现在我想要做的是动态更改线性布局的背景颜色,但在可绘制资源内。我的代码看起来像:

//  bcd.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item>
<shape>
<gradient
android:endColor="#22000000"
android:startColor="#22000000"
android:angle="270" />
<stroke
android:width="3dp"
android:color="@color/white" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>

<LinearLayout 
android:id="@+id/lin_llt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>

我在我的 Activity 中为线性布局设置背景......

parentLayout = (LinearLayout) view.findViewById(R.id.lin_llt);
parentLayout.setBackgroundResource(R.drawable.bcd);

现在我想做的是改变我的可绘制资源的颜色,这意味着改变我的线性布局的颜色,圆角和填充在可绘制中定义..

我尝试了以下方式

ShapeDrawable bgShape = (ShapeDrawable )parentLayout.getBackground();
bgShape.getPaint().setColor(Color.BLACK);

但它不适合我。任何其他解决方案。

那该怎么做呢...需要帮忙...谢谢...

最佳答案

动态改变布局颜色

LinearLayout Layout = (LinearLayout) findViewById(R.layout.id);
Layout.setBackgroundColor(Color.parseColor("#ffffff"));

动态设置背景色渐变

View layout = findViewById(R.id.mainlayout);

GradientDrawable gd = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,
new int[] {0xFF616261,0xFF131313});
gd.setCornerRadius(0f);

layout.setBackgroundDrawable(gd);

关于android - 如何在android中更改可绘制形状的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15127351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com