【发布时间】:2021-08-17 20:03:36
【问题描述】:
编辑:代码一切正常,Android Studio 未部署对应用版本 4.1.3 的更改
我正在尝试在运行时更改视图的背景颜色(相对布局和 rootView)
但这里的代码根本不起作用
rootview = findViewById(R.id.rootView)
rootview.setBackgroundColor(resources.getColor(R.color.backgroundColor2))
布局代码/文件,我在这里使用 relativeLayout 作为主要布局,它被另一个视图完全覆盖
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/rootView"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/banner_container"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
/>
<at.markushi.ui.CircleButton
android:id="@+id/button"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_centerInParent="true"
android:src="@drawable/ic_launcher_foreground"
app:cb_color="@color/mainButtonColor1"
app:cb_pressedRingWidth="8dip" />
<at.markushi.ui.CircleButton
android:layout_width="64dp"
android:elevation="8dp"
android:layout_height="64dp"
android:src="@drawable/ic_launcher_foreground"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="30dp"
android:layout_marginBottom="30dp"
app:cb_color="#B71C1C"
app:cb_pressedRingWidth="8dip" />
</RelativeLayout>
如果我在 XML 中设置背景,它可以正常工作
【问题讨论】:
-
试试
ContextCompat.getColor(this, R.color.yourColor) -
还是不行
-
@TehleelMir - 您在哪个生命周期方法中设置背景颜色?
-
我正在测试 onCreate 方法,但它根本不起作用,在 java 中我曾经做同样的事情并且它曾经工作正常,但在 kotlin 中很奇怪
标签: android kotlin background-color