Android 透明TextView?

发布于 2024-11-19 00:55:35 字数 1327 浏览 3 评论 0 原文

简单地说,如何使 TextView 透明?(不是完全透明)

我搜索了文档和 StackNetwork 但找不到它? 我想有这样的事情。

谢谢。

更新

这是XML代码:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:background="@drawable/background">

    <ImageView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/header"
    android:src="@drawable/logo1"
    />
    <ListView android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:paddingRight="5dp"
    android:scrollbarStyle="outsideOverlay"
    android:cacheColorHint="#00000000" />


    <TextView
    android:id="@+id/footer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25sp"
    android:singleLine="true"
    android:background="#07000000"
    android:textColor="#FFFFFF"
    android:text="rrrrr" />

 </LinearLayout>

我希望页脚 TextView是透明的以便在滚动时可以看到ListView项目< /代码>

Simply, how to make a TextView transparent? (Not full transparency)

I searched the docs and the StackNetwork and couldn't find it?
I guess there is something like this.

Thanks.

UPDATE

This is the XML code:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:background="@drawable/background">

    <ImageView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/header"
    android:src="@drawable/logo1"
    />
    <ListView android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:paddingRight="5dp"
    android:scrollbarStyle="outsideOverlay"
    android:cacheColorHint="#00000000" />


    <TextView
    android:id="@+id/footer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25sp"
    android:singleLine="true"
    android:background="#07000000"
    android:textColor="#FFFFFF"
    android:text="rrrrr" />

 </LinearLayout>

I want the footer TextView to be transparent so that the ListView items can be seen while scrolling

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

江南月 2024-11-26 00:55:35

请参阅 Android Color 资源文档以供参考。

基本上,您可以选择直接在布局中或使用资源引用设置透明度(不透明度)颜色。

您设置的十六进制值由 3 到 4 个部分组成:

  • Alpha(不透明度),我将其称为 aa

  • < p>红色,我将其称为 rr

  • 绿色,我将其称为 gg

    绿色,我将其称为 gg

  • 蓝色,我将其称为 bb

不带 alpha(透明度)值:

android:background="#rrggbb"

或作为资源:

<color name="my_color">#rrggbb</color>

alpha(透明度)值:

android:background="#aarrggbb"

或作为资源:

<color name="my_color">#aarrggbb</color>

完全透明度的 alpha 值为00透明度的 alpha 值为透明度为FF

请参阅下面的完整十六进制值:

100% — FF
 95% — F2
 90% — E6
 85% — D9
 80% — CC
 75% — BF
 70% — B3
 65% — A6
 60% — 99
 55% — 8C
 50% — 80
 45% — 73
 40% — 66
 35% — 59
 30% — 4D
 25% — 40
 20% — 33
 15% — 26
 10% — 1A
  5% — 0D
  0% — 00

您可以尝试使用这些值之间的值。

See the Android Color resource documentation for reference.

Basically you have the option to set the transparency (opacity) and the color either directly in the layout or using resources references.

The hex value that you set it to is composed of 3 to 4 parts:

  • Alpha (opacity), i'll refer to that as aa

  • Red, i'll refer to it as rr

  • Green, i'll refer to it as gg

  • Blue, i'll refer to it as bb

Without an alpha (transparency) value:

android:background="#rrggbb"

or as resource:

<color name="my_color">#rrggbb</color>

With an alpha (transparency) value:

android:background="#aarrggbb"

or as resource:

<color name="my_color">#aarrggbb</color>

The alpha value for full transparency is 00 and the alpha value for no transparency is FF.

See full range of hex values below:

100% — FF
 95% — F2
 90% — E6
 85% — D9
 80% — CC
 75% — BF
 70% — B3
 65% — A6
 60% — 99
 55% — 8C
 50% — 80
 45% — 73
 40% — 66
 35% — 59
 30% — 4D
 25% — 40
 20% — 33
 15% — 26
 10% — 1A
  5% — 0D
  0% — 00

You can experiment with values in between those.

当爱已成负担 2024-11-26 00:55:35

下面的黑色代码:-

<color name="black">#000000</color>

现在,如果我想使用不透明度,您可以使用下面的代码:-

 <color name="black">#99000000</color> 

以及下面的不透明度代码:-

十六进制不透明度值

100% — FF
95% — F2
90% — E6
85% — D9
80% — CC
75% — BF
70% — B3
65% — A6
60% — 99
55% — 8C
50% — 80
45% — 73
40% — 66
35% — 59
30% — 4D
25% — 40
20% — 33
15% — 26
10% — 1A
5% — 0D
0% — 00

refer 了解 Android 上的颜色(六个字符)

Below code for black:-

<color name="black">#000000</color>

Now if i want to use opacity than you can use below code :-

 <color name="black">#99000000</color> 

and below for opacity code:-

Hex Opacity Values

100% — FF
95% — F2
90% — E6
85% — D9
80% — CC
75% — BF
70% — B3
65% — A6
60% — 99
55% — 8C
50% — 80
45% — 73
40% — 66
35% — 59
30% — 4D
25% — 40
20% — 33
15% — 26
10% — 1A
5% — 0D
0% — 00

refer Understanding colors on Android (six characters)

无声情话 2024-11-26 00:55:35

请尝试这段代码。

<TextView 
   android:id="@+id/txtview1"
   android:layout_width="wrap_content" 
   android:background="@drawable/bg_task" 
   android:layout_height="wrap_content" 
   android:textSize="14sp" 
   android:singleLine="true"
   android:textColor="#FFFFFF" />

使用背景图像作为透明,因此可以解决这个问题。

android:background="#07000000"

请尝试以下...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:gravity="center_horizontal" android:orientation="vertical"
    android:background="@drawable/main_bg">
    <ImageView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:id="@+id/header"
        android:src="@drawable/btn_complete" />
    <RelativeLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ListView android:id="@+id/list" android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:layout_weight="1"
            android:paddingRight="5dp" android:scrollbarStyle="outsideOverlay"
            android:cacheColorHint="#00000000" />
        <TextView android:id="@+id/footer" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:textSize="25sp"
            android:singleLine="true" android:background="#07000000"
            android:textColor="#FFFFFF" android:text="rrrrr"
            android:layout_centerInParent="true"
            android:layout_alignParentBottom="true" />
    </RelativeLayout>
</LinearLayout>

Please try this piece of code..

<TextView 
   android:id="@+id/txtview1"
   android:layout_width="wrap_content" 
   android:background="@drawable/bg_task" 
   android:layout_height="wrap_content" 
   android:textSize="14sp" 
   android:singleLine="true"
   android:textColor="#FFFFFF" />

Used background image as transparent so may be solved that.

OR

android:background="#07000000"

OR

Please try below ...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:gravity="center_horizontal" android:orientation="vertical"
    android:background="@drawable/main_bg">
    <ImageView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:id="@+id/header"
        android:src="@drawable/btn_complete" />
    <RelativeLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ListView android:id="@+id/list" android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:layout_weight="1"
            android:paddingRight="5dp" android:scrollbarStyle="outsideOverlay"
            android:cacheColorHint="#00000000" />
        <TextView android:id="@+id/footer" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:textSize="25sp"
            android:singleLine="true" android:background="#07000000"
            android:textColor="#FFFFFF" android:text="rrrrr"
            android:layout_centerInParent="true"
            android:layout_alignParentBottom="true" />
    </RelativeLayout>
</LinearLayout>
灰色世界里的红玫瑰 2024-11-26 00:55:35

使用这个:

android:background="@android:color/transparent"

Use this:

android:background="@android:color/transparent"
比忠 2024-11-26 00:55:35

例如,

<TextView android:alpha="0.3" ..., for example.

怪我鬧 2024-11-26 00:55:35

以编程方式设置:

setBackgroundColor(Color.TRANSPARENT);

对我来说,我还必须在父布局上将其设置为 Color.TRANSPARENT。

To set programmatically:

setBackgroundColor(Color.TRANSPARENT);

For me, I also had to set it to Color.TRANSPARENT on the parent layout.

浅忆 2024-11-26 00:55:35

虽然这个答案很晚,但可能会帮助其他开发人员,所以我将其发布在这里。

上面的答案仅显示如何设置 TextView 的透明背景。我们可以通过两种方式实现透明的 Textview backcgorund:

  1. 通过在 android:background 属性中设置不透明代码,例如 #88000000
  2. 通过为 TextView 设置 android:alpha="0.5" 属性

第二种方法更好,因为它可以灵活地设置不同颜色的背景和 设置小部件的不透明度

然后使用 android:alpha="0.2"示例

<TextView
        android:id="@+id/tv_name"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/black"
        android:alpha="0.3"
        android:textColor="@color/white"
        android:textStyle="bold"/>

Although this answer is very late but might help other developer so I'm posting it here.

Answers above showing only how to set transparent background of TextView. We can achieve transparent Textview backcgorund in two way:

  1. By setting opacity code such as #88000000 in android:background attribute
  2. By setting android:alpha="0.5" attribute to TextView

Second approach is better because it gives flexibility to set background with different color and then setting setting opacity to widget using android:alpha="0.2"

Example

<TextView
        android:id="@+id/tv_name"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/black"
        android:alpha="0.3"
        android:textColor="@color/white"
        android:textStyle="bold"/>
撧情箌佬 2024-11-26 00:55:35

如果您正在寻找类似脉冲应用程序上图像上的文本视图的内容,请执行以下操作

 android:background="#88000000"
android:textColor="#ffffff"

If you are looking for something like the text view on the image on the pulse app do this

 android:background="#88000000"
android:textColor="#ffffff"
筑梦 2024-11-26 00:55:35

这个问题有很多答案,但只有少数是容易理解的。

总结一下并说清楚:


XML 中设置颜色透明的最佳解决方案是使用:
作者:@darvinda

android:background="@android:color/transparent"

使用Java代码设置它的最简单的解决方案将是:
作者:@Aaron

setBackgroundColor(Color.TRANSPARENT);

并解释什么是alpha 通道 (“颜色的透明度”)为:

  • Alpha 是颜色的透明度
  • 将 Alpha 值设置为 00(十六进制)以使任何颜色完全不透明
  • 设置将 Alpha 值设置为 ff(十六进制)以使其透明(颜色不再重要)。

因此,Alpha 通道表示 alpha-rgb 颜色空间 中的透明度。要使某些内容透明,您使用的 rgb 颜色并不重要,只要将“alpha”颜色设置为 00 即可。

十六进制 中的值(十六进制)。 org/wiki/RGBA_color_model" rel="nofollow noreferrer">alpha-rgb 色彩空间 是:
“#aarrggbb”(a=alpha,r=红色,g=绿色 b=蓝色)

This question has many answers, but only a few are easy to understand.

Just to sum it up and make it clear:


The best solution to set a color transparent in XML would be to use:
(by @darvinda)

android:background="@android:color/transparent"

And easiest solution to set it with Java code would be:
(by @Aaron)

setBackgroundColor(Color.TRANSPARENT);

And to explain what the alpha channel ("the transparency of the color") is:

  • Alpha is the transparency of the color
  • Set the Alpha value to 00 (hex) to make any color fully opaque
  • Set the Alpha value to ff (hex) to make it transparent (the color doesn't matter anymore).

So the alpha channel represents the degree of transparency in an alpha-rgb color space. To make something transparent it doesn't matter with rgb-color you use, as long as the "alpha"-color is set to 00.

The values (hex) in an alpha-rgb color space are:
"#aarrggbb" (a=alpha, r=red, g=green b=blue)

游魂 2024-11-26 00:55:35

尝试在 TextView 中设置 android:background="#00000000" 。设置颜色 00 的 alpha 将使背景透明。

我没有尝试过这个,但它应该有效。

Try setting android:background="#00000000" in TextView. Setting alpha of colour 00 will make the background transparent.

I haven't tried this, but it should work.

献世佛 2024-11-26 00:55:35

每个人都正确地回答了透明度问题,但没有听取这个人对页脚后面滚动列表的需求。

您需要将页脚作为 ListView 的一部分。目前,列表不会向后滚动,因为 ListView 的布局不会在透明的页脚视图后面。创建一个 RelativeLayout 并将透明度放置在底部。

Everyone is answering correctly about the transparency but not listening to what the guy needs in regards to the list scrolling behind the footer.

You need to make the footer part of your ListView. At the moment the list won't scroll behind because the layout of the ListView does not go behind the footer view with the transparency. Make a RelativeLayout and position the transparency at the bottom.

肤浅与狂妄 2024-11-26 00:55:35

万一有人想以编程方式做到这一点!
执行以下操作:
用此更新值文件夹中的颜色文件。

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="transparent">#00ffffff</color> <!--This is the transparency line-->
</resources>

然后以编程方式调用透明度

your_textview.setBackgroundResource(R.color.transparent);

In case someone wants to do this in the programming way!
Do the following:
Update the color file in your values folder with this.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="transparent">#00ffffff</color> <!--This is the transparency line-->
</resources>

then call the transparency programitically

your_textview.setBackgroundResource(R.color.transparent);
牵你的手,一向走下去 2024-11-26 00:55:35

尝试使用 android-studio 设计器在 Activity_main.xml 中设置透明度。
如果您希望它是透明的,请像这样编写白色:
白色:#FFFFFF,50% 透明度:#80FFFFFF
这是针对 Kotlin 的,不确定这对于基本的 android (java) 是否会以同样的方式工作。

try to set the transparency with the android-studio designer in activity_main.xml.
If you want it to be transparent, write it for example like this for white:
White: #FFFFFF, with 50% transparency: #80FFFFFF
This is for Kotlin tho, not sure if that will work the same way for basic android (java).

铁憨憨 2024-11-26 00:55:35

您好,请尝试使用以下颜色代码作为文本视图的背景。

android:background="#20535252"

Hi Please try with the below color code as textview's background.

android:background="#20535252"

把梦留给海 2024-11-26 00:55:35
setBackgroundColor(Color.TRANSPARENT);

最简单的方法

setBackgroundColor(Color.TRANSPARENT);

The simplest way

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文