gpt4 book ai didi

java - 从代码更改布局的背景颜色

转载 作者:行者123 更新时间:2023-11-29 07:43:24 25 4
gpt4 key购买 nike

我有一个 View ,它具有定义的可绘制背景(使它成为一个圆圈)并为其提供基本背景颜色:
circle_block.xml:

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

<solid
android:color="#aaaaaa" />
</shape>

activity_main.xml 中:

<LinearLayout
android:id="@+id/result_container"
android:background="@drawable/circle_block"
android:orientation="vertical"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginTop="15dp"
android:gravity="center"
android:layout_gravity="center">

我尝试通过代码仅更改背景颜色,但没有成功。
MainActivity.java:

private LinearLayout container;
// on create
container = (LinearLayout) findViewById(R.id.result_container);
container.setBackgroundColor(0x4CAF50);

但它只是变白了。保持椭圆形很重要。

最佳答案

在 res/values 文件夹中创建 colors.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="blue">#ff64c2f4</color>
</resources>

然后:

container.setBackgroundColor(getResources().getColor(R.color.blue));

关于java - 从代码更改布局的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27943449/

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