在运行时向活动背景添加半透明覆盖层。

6

我有一个应用程序,我从xml中设置了背景图片。我通过调用以下命令获取视图:

setContentView(R.)

在运行时,如何根据条件在此背景上放置半透明的覆盖层。我想要一个红色的覆盖层,其alpha值设置为50%。
我尝试创建一个单独的XML文件,其中视图被复制并带有不同的图像/覆盖层,但这很混乱,因为当我使用新视图时,我必须充气所有按钮/文本视图。
谢谢Matt。
[编辑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"
         />

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

如果您知道您需要多少不透明度,为什么不使用PNG格式呢?这将是一个图像,您可以在需要时添加到布局中。 - Prmths
1个回答

20

就我个人而言,你可以在资源文件(即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的可见性。


嗨,我已经发布了我的XML。如你所见,我设置了一个可绘制的背景。在运行时是否可以在这个可绘制对象上面放置一种红色调?你的代码可以实现这个吗?谢谢。 - turtleboy
如果你想让红色叠加层覆盖在所有东西上面,包括你的可绘制对象,那么我的代码片段就是这样做的。如果你只想给你的可绘制对象应用红色滤镜,那就是完全不同的故事了。 - ssantos
如果您想在可绘制对象上方添加红色叠加层,可以将@+id/transparentOverlay布局放置在可绘制对象视图的上方。 - ssantos
嗨,我已经将覆盖图层放在了可绘制对象的上方,但它并没有显示出来。如edit2所示。你有任何想法为什么会这样,或者这不是你想说的吗?谢谢。 - turtleboy
嗨,我在edit3中用了一种不同的方法。我保留了原始布局代码,但为LL布局分配了一个ID。我创建了第二个可绘制对象,并对其进行了alpha和red效果处理。然后,根据条件在运行时切换布局的可绘制对象。谢谢你的帮助。现在我这样做可以吗? - turtleboy
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接