首页 > 解决方案 > 如何在 Android Studio 中将 alpha 属性设置为图像而不是文本?

问题描述

我使用线性布局来平均分布视图组的空间。我将视图组背景设置为图像并将 alpha 属性添加到线性布局。不透明度应用于视图组中的所有视图。但我希望 alpha 属性应仅应用于背景图像而不应用于 textview。有没有办法在线性布局中做到这一点。

<LinearLayout
    android:layout_height="300dp"
    android:layout_width="match_parent"
    android:layout_below="@id/getCoffeeText"
    android:background="@drawable/coffee"
    android:layout_marginLeft="20sp"
    android:layout_marginRight="20sp"
     android:alpha="0.5"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20sp"
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Order Coffee Here....!!!!"
        android:textColor="#D50000"
        android:textSize="20sp"
        android:layout_marginLeft="60sp"/>
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal">
        <Button
            android:text="+"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:onClick="incrementCoffeeCount"
            />
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:padding="45sp"
            android:text="0"
            android:textSize="20sp"
            android:textColor="#D50000"
            android:id="@+id/coffeeCount"
            />
        <Button
            android:text="-"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:onClick="decrementCoffeeCount"
            />
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Price"
        android:textSize="25sp"
        android:textColor="#D50000"
        android:layout_marginLeft="130sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="$0"
        android:textSize="20sp"
        android:textColor="#D50000"
        android:layout_marginLeft="130sp"
        android:id="@+id/price"/>

    <Button
        android:text="Order"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginLeft="110sp"
        android:onClick="submitOrder"/>
   </LinearLayout>

我想在linearlayout而不是Relativelayout中执行此操作。任何人请帮助我。在此先感谢。

标签: androidandroid-linearlayout

解决方案


尝试使用FrameLayout如下代码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="0.5"
        android:layout_gravity="center"
        android:background="@drawable/coffee" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="Hello World!"
            android:textColor="@color/orange"
            android:textSize="30sp" />
    </LinearLayout>

</FrameLayout>

推荐阅读