在运行时将半透明叠加添加到活动背景

时间:2013-09-27 15:45:00

标签: android image view

我有一个应用程序,我将背景设置为xml中的图像。我通过调用

来获取视图
setContentView(R.)

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

我尝试创建一个单独的xml文件,其中包含重复的视图和不同的图像/叠加层,但是当我使用新视图时,我必须对所有按钮/ textview进行充气。

感谢亚光

[EDIT1]

<?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"
         />

[EDIT2]

<?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"
         />

[EDIT3]

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

1 个答案:

答案 0 :(得分:19)

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

<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的可见性。