gpt4 book ai didi

Android:同时显示背景颜色和图像

转载 作者:太空狗 更新时间:2023-10-29 15:46:13 26 4
gpt4 key购买 nike

我需要通过 java 而不是 xml 获得 TextView 背景的颜色和图像。换句话说,我需要这样的 css 规则:

background:#f00 url(...);

如何在不使用 xml 的情况下实现这一目标?

谢谢

最佳答案

你不能没有 xml。

第一步

新建Drawable资源文件textview_background.xml

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">


<item android:id="@+id/background" >
<shape android:shape="rectangle">
<solid android:color="COLOR"/>
</shape>
</item>

<item android:id="@+id/item_img">
<bitmap android:src="IMAGE" />
</item>

</layer-list>

第 2 步

在布局 xml 文件中:

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/textview_background" />

第 3 步

如果您想以编程方式自定义背景:

// change background image
LayerDrawable layer = (LayerDrawable) textView.getBackground();
BitmapDrawable bitmap = (BitmapDrawable) resources.getDrawable(R.drawable.IMAGE);
layer.setDrawableByLayerId(R.id.item_img, bitmap);
// change background color
GradientDrawable bgShape = (GradientDrawable) textView.getBackground();
bgShape.setColor(Color.YOURCOLOR);

关于Android:同时显示背景颜色和图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24265648/

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