6

我有一个应用程序,我将背景设置为来自 xml 的图像。我通过调用获得视图

setContentView(R.)

. 如何根据条件在运行时在此背景上放置半透明覆盖。我想要一个红色叠加层,其 alpha 设置为 50%。

我尝试创建一个单独的 xml 文件,其中包含重复的视图和不同的图像/叠加层,但它很混乱,因为当我使用新视图时我必须为所有按钮/文本视图充气。

谢谢马特

[编辑1]

<?xml version="1.0" encoding="utf-8"?>

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


    <TextView
        android:id="@+id/textviewcompanyname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#003F87" />

    <TextView
        android:id="@+id/textViewYouAreSignedIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_alignParentTop="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#003F87"
         />

          <TextView
        android:id="@+id/textViewUnsentTransactions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_below="@id/textViewYouAreSignedIn"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#003F87"
         />

[编辑2]

<?xml version="1.0" encoding="utf-8"?>


 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >


     <RelativeLayout
        android:id="@+id/transparentOverlay"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/red_transparent" >


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




    <TextView
        android:id="@+id/textviewcompanyname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#003F87" />

    <TextView
        android:id="@+id/textViewYouAreSignedIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_alignParentTop="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#003F87"
         />

[编辑3]

<?xml version="1.0" encoding="utf-8"?>


 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >



<LinearLayout 
    android:id="@+id/ll1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/carefreebgscaledlighting"
    android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">




    <TextView
        android:id="@+id/textviewcompanyname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#003F87" />

.

if(status.equalsIgnoreCase(IN)){

                youAreSignedInAt.setText("You are signed in at " + name);
                LinearLayout layout =(LinearLayout)findViewById(R.id.ll1);
                layout.setBackgroundResource(R.drawable.carefreebgscaledlightingred);
            }else{

                youAreSignedInAt.setText("You are not signed in");
                LinearLayout layout =(LinearLayout)findViewById(R.id.ll1);
                layout.setBackgroundResource(R.drawable.carefreebgscaledlighting);
            }
4

1 回答 1

20

在我的脑海中,您可以在资源文件(即colors.xml)中声明包括alpha组件的背景颜色

<color name="red_transparent">#66FF0000</color>

RelativeLayout然后在其余视图之上放置一个视图(即)。

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

    <!-- All your views -->

    <!-- Transparent layer -->   
    <RelativeLayout
        android:id="@+id/transparentOverlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/red_transparent" />

</RelativeLayout>

您需要做的就是根据需要更改 R.id.transparentOverlay 的可见性。

于 2013-09-27T15:51:52.297 回答