【问题标题】:Android Transparent TextView?Android透明TextView?
【发布时间】:2011-07-07 10:10:11
【问题描述】:

简单地说,如何使 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 是透明的 so that the ListView items can be seen while scrolling

【问题讨论】:

  • 我有一个 ListView,我在最底部添加了一个 TextView。我希望这个 TextView 是透明的,以便在滚动时可以看到 ListView 项。
  • @ChiragRaval:这是最没有建设性的评论。这显然是一个有效的问题。请不要再发表类似的评论。

标签: android transparency transparent textview


【解决方案1】:

请参阅Android Color resource documentation 以供参考。

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

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

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

  • 红色,我称它为rr

  • 绿色,我称它为gg

  • 蓝色,我称它为bb

没有 alpha(透明度)值:

android:background="#rrggbb"

或作为资源:

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

使用 alpha(透明度)值:

android:background="#aarrggbb"

或作为资源:

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

完全透明度的 alpha 值为 00no 透明度的 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

您可以尝试介于两者之间的值。

【讨论】:

  • 真的很棒。我现在明白了。但是,我不能让ListView 的项目出现在TextView 后面!使TextView 透明的全部目的是让列表项出现在它后面。请看一下 XML 代码并告诉我有什么问题?
  • @Videre 非常感谢您提供了一个很好的答案
【解决方案2】:

下面的黑色代码:-

<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

转介 Understanding colors on Android (six characters)

【讨论】:

  • 谢谢 - 这是一个很棒的指南!
【解决方案3】:

请试试这段代码..

<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>

【讨论】:

  • 不起作用!我用我的 XML 代码 including yours also 更新了这个问题。请检查一下
  • #07000000 现在可以使用了。但是我还是不能让ListView的物品出现在它后面!有什么想法吗?
  • 真的很棒。你解决它伙计:D。您只需将其添加到TextView 标签:android:layout_alignParentBottom="true" 以将其与按钮对齐。谢谢@Nik
【解决方案4】:

使用这个:

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

【讨论】:

  • 这个是最好的,因为以后再读时很容易知道代码“颜色/透明”的作用。
【解决方案5】:

例如&lt;TextView android:alpha="0.3" ...

【讨论】:

  • 简单但仍然可以完成工作。很好的解决方案。
【解决方案6】:

以编程方式设置:

setBackgroundColor(Color.TRANSPARENT);

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

【讨论】:

    【解决方案7】:

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

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

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

    第二种方法更好,因为它可以灵活地设置不同颜色的背景,然后使用 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"/>
    

    【讨论】:

      【解决方案8】:

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

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

      【讨论】:

        【解决方案9】:

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

        我还没有尝试过,但它应该可以工作。

        【讨论】:

        • 你的意思是:Setting alpha of colour 00
        • alpha(或透明)颜色由“aarrggbb”(a=alpha, r=red, g=green b=blue)颜色空间中 # 之后的第一个十六进制数字设置.所以任何以#00 开头的颜色都是完全透明的。
        【解决方案10】:

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

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

        【讨论】:

          【解决方案11】:

          如果有人想以编程方式执行此操作! 请执行下列操作: 用这个更新你的 values 文件夹中的颜色文件。

          <?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);
          

          【讨论】:

            【解决方案12】:

            尝试使用 android-studio 设计器在 activity_main.xml 中设置透明度。 如果您希望它是透明的,请为白色编写如下示例: 白色:#FFFFFF,50% 透明度:#80FFFFFF 这适用于 Kotlin,但不确定这是否适用于基本的 android (java)。

            【讨论】:

              【解决方案13】:

              这个问题有很多答案,但只有几个容易理解。

              只是总结一下,说清楚:


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

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

              使用 Java 代码 进行设置的最简单解决方案是:
              (by @Aaron)

              setBackgroundColor(Color.TRANSPARENT);
              

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

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

              所以 Alpha 通道代表alpha-rgb color space 的透明度。为了使某些东西透明,您使用的 rgb-color 无关紧要,只要“alpha”-color 设置为 00

              alpha-rgb color space 中的值 (hex) 是:
              "#aarrggbb" (a=alpha, r=red, g=green b=blue)

              【讨论】:

                【解决方案14】:

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

                android:background="#20535252"

                【讨论】:

                • 效果很好。它使TextView 透明。但是我还是不能让ListView的物品出现在它后面!有什么帮助吗?
                • 你能把你的屏幕截图贴出来吗?
                【解决方案15】:
                setBackgroundColor(Color.TRANSPARENT);
                

                最简单的方法

                【讨论】:

                  猜你喜欢
                  • 2013-02-11
                  • 1970-01-01
                  • 2012-02-08
                  • 2017-01-05
                  • 2020-02-23
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2012-12-17
                  相关资源
                  最近更新 更多