Android 运行时背景颜色的变化
我正在使用LinearLayout设置具有曲线角的背景形状。我已经创建了可绘制的XML文件。当我在“活动”中尝试在运行时更改LinearLayout背景颜色时,该颜色会显示在布局中,但未应用背景形状。需要帮忙吗 My layout.xml文件:Android 运行时背景颜色的变化,android,background-color,Android,Background Color,我正在使用LinearLayout设置具有曲线角的背景形状。我已经创建了可绘制的XML文件。当我在“活动”中尝试在运行时更改LinearLayout背景颜色时,该颜色会显示在布局中,但未应用背景形状。需要帮忙吗 My layout.xml文件: <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/month_card"
android:backgroundTint="@drawable/circle_corner_rectangle"
app:backgroundTintMode="src_over">
试试这个代码
circle\u corner\u rectangle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#E0F2F1" />
<corners android:radius="6dp"/>
</shape>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/month_card"
android:backgroundTint="@drawable/circle_corner_rectangle">
使用
也许这对你的情况有用
val shape = GradientDrawable()
shape.shape = GradientDrawable.RECTANGLE
shape.setStroke(mStrokeWidth!!,mStrokeColor!!)
shape.cornerRadius = 2f
imageView.background = shape
此代码在kotlin中
试试这个,
layout.setBackgroundColor(Color.parseColor("#20A4E8"));
(or)
layout.setBackgroundColor(Color.BLUE);
只需添加另一个所需的xml文件,并在运行时添加此代码
layout.setBackgroundTintList(getContext.getResources().getColorStateList(R.color.your_xml_name));
试试这个:
Drawable drawable = yourView.getBackground();
try {
drawable.setColorFilter(Color.parseColor(yourColor), PorterDuff.Mode.MULTIPLY);
} catch (Exception e) {
e.printStackTrace();
}
是否要更改布局背景或背景色调?如何在运行时设置颜色SetBackground(ContextCompat.getDrawable(activity,R.drawable.circle\u corner\u rectangle)我不使用kotlin broi不使用kotlinUse GradientDrawable()用于运行时颜色更改。该代码仅供参考。但我是从arraylist设置颜色的,未定义可绘制形状colour@karthik就这么做吧,我会试试的,兄弟。
val shape = GradientDrawable()
shape.shape = GradientDrawable.RECTANGLE
shape.setStroke(mStrokeWidth!!,mStrokeColor!!)
shape.cornerRadius = 2f
imageView.background = shape
layout.setBackgroundColor(Color.parseColor("#20A4E8"));
(or)
layout.setBackgroundColor(Color.BLUE);
layout.setBackgroundTintList(getContext.getResources().getColorStateList(R.color.your_xml_name));
Drawable drawable = yourView.getBackground();
try {
drawable.setColorFilter(Color.parseColor(yourColor), PorterDuff.Mode.MULTIPLY);
} catch (Exception e) {
e.printStackTrace();
}