防止 RelativeLayout 环绕背景图像
Prevent RelativeLayout from wrapping around the background image
我有一个 RelativeLayout。我想在上面应用阴影效果,所以我定义了一个自定义背景 (updatebg.xml):
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/background" >
<shape android:shape="rectangle">
<solid android:color="#449966"/>
</shape>
</item>
<item android:id="@+id/shadow">
<bitmap android:src="@drawable/shadow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</item>
</layer-list>
和 RelativeLayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/layoutP"
android:background="@drawable/updatebg"
>
</RelativeLayout>
一切正常,除了 RelativeLayout 被拉伸到与背景图像高度(阴影)相同的高度。如果我在 RelativeLayout 中的内容占用的高度小于阴影背景图像的高度,那么 RelativeLayout 也将被拉伸到 "wrap around the background",而不是仅环绕 RelativeLayout 中的内容。
我需要将背景图像调整到父级高度 (RelativeLayout) 或被裁掉。有关如何完成此操作的任何提示?谢谢!
我正在尝试从上到下应用渐变类型的阴影:
编辑
可能我理解错了你说的"shadow effect",如果你只是想要一个渐变,使用<gradient>
drawable。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/background" >
<shape android:shape="rectangle">
<solid android:color="#449966"/>
</shape>
</item>
<item>
<shape
android:dither="true"
android:shape="rectangle" >
<gradient
android:angle="270"
android:endColor="#7fffffff"
android:startColor="#7f000000" />
</shape>
</item>
</layer-list>
结果是这样的:
原答案
您应该考虑改用 9-Patch
png。是这样的:
这只是一张普通的 png 图像,但带有一些附加标记(边框上的黑色 points/lines),Android 将使用这些标记根据需要拉伸图像。
有关如何使用 9-Patch
文件的更多详细信息,请参阅 the official docs。
我有一个 RelativeLayout。我想在上面应用阴影效果,所以我定义了一个自定义背景 (updatebg.xml):
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/background" >
<shape android:shape="rectangle">
<solid android:color="#449966"/>
</shape>
</item>
<item android:id="@+id/shadow">
<bitmap android:src="@drawable/shadow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</item>
</layer-list>
和 RelativeLayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/layoutP"
android:background="@drawable/updatebg"
>
</RelativeLayout>
一切正常,除了 RelativeLayout 被拉伸到与背景图像高度(阴影)相同的高度。如果我在 RelativeLayout 中的内容占用的高度小于阴影背景图像的高度,那么 RelativeLayout 也将被拉伸到 "wrap around the background",而不是仅环绕 RelativeLayout 中的内容。
我需要将背景图像调整到父级高度 (RelativeLayout) 或被裁掉。有关如何完成此操作的任何提示?谢谢!
我正在尝试从上到下应用渐变类型的阴影:
编辑
可能我理解错了你说的"shadow effect",如果你只是想要一个渐变,使用<gradient>
drawable。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/background" >
<shape android:shape="rectangle">
<solid android:color="#449966"/>
</shape>
</item>
<item>
<shape
android:dither="true"
android:shape="rectangle" >
<gradient
android:angle="270"
android:endColor="#7fffffff"
android:startColor="#7f000000" />
</shape>
</item>
</layer-list>
结果是这样的:
原答案
您应该考虑改用 9-Patch
png。是这样的:
这只是一张普通的 png 图像,但带有一些附加标记(边框上的黑色 points/lines),Android 将使用这些标记根据需要拉伸图像。
有关如何使用 9-Patch
文件的更多详细信息,请参阅 the official docs。