【问题标题】:Opaque Layer on top of semi-transparent layer半透明层之上的不透明层
【发布时间】:2015-12-30 06:49:23
【问题描述】:

我有一个按我想要的布局的活动。当用户单击按钮时,我将显示一个弹出的编辑文本框。我想在弹出的edittext和半透明的主要层之间放置一个中间层,以保持用户对弹出窗口的关注。

所以我将中间层的 alpha 值设置为 0.5。然而,弹出层现在也呈现为半透明,即使我明确将其 alpha 设置为 1.0。

<!-- this is the middle layer (I call it the 'curtain') -->
<LinearLayout
    android:id="@+id/log_entry_reply_container_curtain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:alpha="0.5"
    android:visibility="gone">

    <!-- this is the popup layer -->
    <RelativeLayout
        android:id="@+id/log_entry_reply_container"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:layout_gravity="center"
        android:alpha="1.0"
        style="@style/RoundedCornersView" >

        <TextView
            android:id="@+id/log_entry_tv_reply"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:textSize="22sp"
            android:layout_marginBottom="5dp"
            android:layout_alignParentTop="true"
            android:text="@string/enter_reply"/>

        <EditText
            android:id="@+id/log_entry_reply_edittext"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_below="@+id/log_entry_tv_reply"
            android:layout_margin="5dp"
            android:gravity="top"/>

    </RelativeLayout>
</LinearLayout>

如何使这个中间层半透明,同时保持顶层完全不透明?感谢您的帮助!

【问题讨论】:

    标签: android android-layout alpha-transparency


    【解决方案1】:

    你好在drawable文件夹中创建semitransperent.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >
    
        <stroke android:width="2dp" />
    
    
    
        <gradient
            android:angle="90"
            android:endColor="#29000000"
            android:startColor="#29000000" />
    
        <corners
            android:bottomLeftRadius="7dp"
            android:bottomRightRadius="7dp"
            android:topLeftRadius="7dp"
            android:topRightRadius="7dp" />
    
    </shape>
    

    Note.Stroke,gradient,corners 根据您需要更改的要求。

    在 xml 代码中添加以下行:

       android:background="@drawable/semitransperent"
    

    【讨论】:

    • 非常感谢。我将此标记为解决方案,因为它有效。但是,对于任何可能使用它的人,我要指出&lt;shape&gt; 的主体不需要任何&lt;stroke&gt;&lt;gradient&gt;&lt;corners&gt; 标记。解决原始问题所需的只是&lt;solid android:color="#cc000000" /&gt;,其中“cc”代表您要应用于背景的任何 alpha 值。谢谢你的建议。绝对有效!
    猜你喜欢
    • 2012-08-03
    • 1970-01-01
    • 2012-01-22
    • 2012-02-17
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多