在Android上的半透明形状上绘制顶部边框



我想将布局的背景设置为具有顶部边框(不透明)的半透明颜色。我试图实现这一目标。目前,我的可绘制文件如下:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#FF0000FF" />
        </shape>
    </item>
    <item android:top="5dp">
        <shape>
            <solid android:color="#7F000000" />
        </shape>
    </item>
</layer-list>

但是,在此XML中,半透明的形状具有非透明形状,因此在结果中根本不是半透明的。您能帮我获得我想要的背景吗?预先感谢。

在搜索网络并尝试找到正确的解决方案后,最后我从聊天中的人那里得到了答案。解决方案是在第一个上方创建一个layout,例如,给它一个10dp的高度和match_parent的宽度。


这是工作示例:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/MainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/main_bg"
    android:orientation="vertical"
    android:padding="5dp"
    tools:context=".MainActivity" >
    <LinearLayout
        android:id="@+id/Box"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background"
        android:orientation="vertical" >
        <LinearLayout
            android:id="@+id/Border"
            android:layout_width="match_parent"
            android:layout_height="10dp"
            android:layout_weight="0.64"
            android:background="@drawable/border"
            android:orientation="vertical"
            tools:ignore="InefficientWeight" >
        </LinearLayout>
        <TextView
            android:id="@+id/Content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.64"
            android:padding="5dp"
            android:text="@string/example"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@color/white" />
    </LinearLayout>
</LinearLayout>

main_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="5dp">
        <shape>
            <solid android:color="#FF990000" />
        </shape>
    </item>
</layer-list>

背景.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="5dp">
        <shape>
            <solid android:color="#55000000" />
        </shape>
    </item>
</layer-list>

border.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="5dp">
        <shape>
            <solid android:color="#FFFF0000" />
        </shape>
    </item>
</layer-list>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Transparent Views</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="example">Content &#8230;</string>
    <color name="white">#FFFFFFFF</color>
</resources>

希望这对别人也会有所帮助。

这是一个简单的解决方案。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:left="-5dp" android:bottom="-5dp" android:right="-5dp">
        <shape android:shape="rectangle">
            <stroke android:width="5dp" android:color="@color/borderColor" />
            <solid android:color="@color/backgroundColor" />
        </shape>
    </item>
</layer-list>

最新更新