Android中的TextView背景变成了黑色而非透明色。

12
以下两张图片是一个包含以下属性的 TextView
<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/button_style"
            android:text="select password"
            android:textColor="@color/dif_black"/>

button_style.xml 则是:

<?xml version="1.0" encoding="UTF-8"?>
<layer-list >
<item>
    <shape>
        <stroke
            android:width="2dp"
            android:color="#88C425" />
        <corners android:radius="20dp" />
    </shape>
</item>

第一张图片是从Canvas 2中拍摄的,第二张图片是从三星Galaxy Fame中拍摄的。这里的问题是我不想让黑色填充在textview边框(描边)内。您可能已经注意到,在第一张图片中,textview的背景是透明的。我希望在所有Android设备上都保持相同的透明背景。

来自Canvas 2的具有透明背景的第一张图片 输入图像描述


你需要指定应用程序的主题,例如Light、NoTitleBar等。 - Imtiyaz Khalani
1
你的TextView的背景颜色可能仍然是透明的。但是如果三星Galaxy Fame的应用程序主题是黑色呢?那么你将会看到一个透明背景的TextView,但是它的文字会显示为黑色,对吧? - VJ Vélan Solutions
终于搞定了。我将以下代码添加到我的样式属性的<Shape>标签中。<solid android:color="@android:color/transparent" /> - Akbarsha
3个回答

16

您仅指定了形状的描边,但它需要有背景。看起来黑色是默认值。

通过添加实心透明背景,更改您的 button_style.xml 形状:

<shape>
    <stroke
        android:width="2dp"
        android:color="#88C425" />
    <corners android:radius="20dp" />
    <solid android:color="@android:color/transparent" />
</shape>

在<Button/>标签中无法使用按钮图像背景。 - David Dimalanta
@DavidDimalanta 感谢您指出这一点,<Button/> 是否有现有的解决方案? - Glib Ischenko

1
对于一个简单的TextView,您可以这样做。
(TextView) txtListChild.setBackgroundColor(Color.TRANSPARENT);

但是,如果您正在使用 ListView,则需要找到用于每个项目的 itemView 布局并在那里设置它... 就像这样

if (convertView == null) {
    LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = infalInflater.inflate(R.layout.list_item, null);
    convertView.setBackgroundColor(Color.TRANSPARENT);
}

-1
如果您需要透明颜色,则需要像这样定义:
android:background="#ffffffff"

例如

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#ffffffff"
            android:text="select password"
            android:textColor="#efyfff"/>

或者你可以将背景设置为白色

android:background="@color/white"

更新:

<Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#ffffffff"
                android:text="select password"
                android:textColor="#efyfff"/>

如果我给定#FFFFFFFF,那么显然它将是白色的。为了看到背景,我必须使用@android:color/transparent,它能够完全达到我的效果。但是我应该将它用在我的button_style.xml中,而不是像你在这里提到的直接用在TextView上。 - Akbarsha
按照上述提到的方法,使用“android:background="#ffffffff"”来设置您的背景。 - AndroidHacker
请检查我的编辑,#FFFFFFFF 不是白色。它代表透明。 - AndroidHacker
2
@AndroidHacker 你测试过颜色代码#FFFFFFFF了吗?它代表的是白色而不是透明色... - Gopal Gopi
给定8F并没有给我透明的背景,只是变成了白色。我使用了@android:color/transparent解决了我的问题。我懂了,无论如何感谢你的回复。 - Akbarsha
好的,它必须给出。尝试使用透明颜色“#00424242”。 - AndroidHacker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接