如何在Android Studio中将alpha属性设置为图像而不是文本



我使用linearlayout来平均分配视图组的空间。我将视图组背景设置为图像,并为linearlayout添加了alpha属性。不透明度应用于视图组中的所有视图。但我希望alpha属性应该只应用于背景图像,而不是文本视图。在linearlayout中有什么方法可以做到这一点吗。

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

我想在线性布局中做这件事,而不是在Relativelayout。任何人都请帮助我。提前谢谢。

尝试使用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>

最新更新