首页 > 解决方案 > 影响 Textview 颜色的父级相对布局前景色

问题描述

我需要一些透明的相对布局背景,因为我使用了渐变选项,但它也会影响文本视图颜色,我给文本视图的颜色是白色,但看起来像灰色。请帮助我摆脱这个问题。

<RelativeLayout
    android:id="@+id/topLayout1"
    android:layout_width="match_parent"
    android:layout_height="210dp"
    android:layout_alignParentTop="true"
    android:foreground="@drawable/image_overlay"
    >

    <RelativeLayout
        android:id="@+id/topLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/preloader_image"
        >
        <TextView
            android:id="@+id/resName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="100dp"
            android:text="Green Way"
            android:textColor="@color/White"
            android:textSize="25sp"
            android:backgroundTint="@color/White"

            />

        <TextView
            android:id="@+id/desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/resName"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="5dp"
            android:text="@string/items"
            android:textColor="@color/White"
            android:textSize="16sp"
             />

    </RelativeLayout>
</RelativeLayout>

和图像叠加代码,

 <?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
    android:startColor="#B0000000"
    android:centerColor="#A0000000"
    android:endColor="#00FFFFFF"
    android:angle="270"
    />
  </shape>

<color name="White">#FFFFFF</color>

标签: android

解决方案


您可以使用#83000000颜色代码来设置透明度,android:background而不是android:background属性。或者,您也可以使用"android:aplha="0.x"where 1 < x < 9 来获得透明度。您的最终代码将如下所示。

<RelativeLayout android:id="@+id/topLayout1"
android:layout_width="match_parent"
android:layout_height="210dp"
android:layout_alignParentTop="true"
android:background="#83000000"
xmlns:android="http://schemas.android.com/apk/res/android">

<RelativeLayout
    android:id="@+id/topLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/resName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp"
        android:text="Green Way"
        android:textColor="#FFFFFF"
        android:textSize="25sp"
        android:backgroundTint="#FFFFFF" />

    <TextView
        android:id="@+id/desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/resName"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dp"
        android:text="Items"
        android:textColor="#FFFFFF"
        android:textSize="16sp" />
</RelativeLayout>

您应该查看此ANSWER以了解有关透明度的更多信息。您可以使用各种十六进制代码来实现不同级别的透明度。就像使用 50% 透明度80代码和颜色代码一样#80000000。同样,您可以在方案中使用各种代码#xx000000


推荐阅读