渐变背景在某些设备上无法正常工作。

4
我将使用渐变可绘制对象来创建图像内阴影,对于一些设备(在模拟器中测试)它能正常工作,但是对于另外一些设备则不能。我最初认为这取决于设备的API级别,但今天我在一个具有API级别“16”(即jelly bean)的平板电脑上进行了相同的测试,它也无法正常工作。
以下是用于所有四个边的渐变代码(用于内阴影):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="rectangle" >
        </shape>
    </item>
    <item
        android:bottom="114dip"
        android:top="0dip">
        <shape android:shape="rectangle" >
            <gradient
                android:angle="270"
                android:endColor="#00000000"
                android:startColor="#40000000" />
        </shape>
    </item>
    <item
        android:bottom="0dip"
        android:top="114dip">
        <shape android:shape="rectangle" >
            <gradient
                android:angle="90"
                android:endColor="#00000000"
                android:startColor="#40000000" />
        </shape>
    </item>
    <item
        android:left="0dip"
        android:right="114dip">
        <shape android:shape="rectangle" >
            <gradient
                android:angle="360"
                android:endColor="#00000000"
                android:startColor="#40000000" />
        </shape>
    </item>
    <item
        android:left="114dip"
        android:right="0dip">
        <shape android:shape="rectangle" >
            <gradient
                android:angle="180"
                android:endColor="#00000000"
                android:startColor="#40000000" />
        </shape>
    </item>

</layer-list>

我的布局代码:
 <FrameLayout
                android:id="@+id/about"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:background="@color/light_green" >

                <ImageView
                    android:id="@+id/home_icon_placeholder"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/home_icon_placeholder" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:drawablePadding="5dp"
                    android:drawableTop="@drawable/about_icon"
                    android:gravity="center"
                    android:text="@string/about_champions_club_btn_txt"
                    android:textColor="@android:color/white"
                    android:textSize="12sp"
                    android:textStyle="bold" />
            </FrameLayout>

我在代码中使用渐变效果:

int sdk = android.os.Build.VERSION.SDK_INT;
        if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
            placeHolderImageViewAbout.setBackgroundColor(getResources().getColor(R.color.transparent));
        } else {
//shadow background is above mention gradient
            placeHolderImageViewAbout.setBackgroundResource(R.drawable.shadow_background);
}

你有检查硬件加速是否启用吗?Android 3.0 硬件加速 如何在ICS上启用硬件加速,但在Gingerbread上禁用?看看shalafi的回答。 - Toris
@Toris - 感谢您的回复,但这个方法也不起作用。 - Manish
1个回答

4

从你的shadow_background.xml中删除此块(第一个项块)。

<item>
    <shape android:shape="rectangle" >
    </shape>
</item>

为了检查shadow_background,我创建了下面这样的布局,它似乎可以工作(即使在API10中的xml编辑器中)。
(如果您保留第一个item块,则LinearLayout将填充为黑色。)

FrameLayout
(Background = @color/light_green)
    LinearLayout
    (Background = @drawable/shadow_background)

耶!!它起作用了...但是你能告诉我为什么在一些设备上这个形状属性不起作用吗? - Manish
3
在一些设备中,形状的填充颜色默认为黑色(不是透明的)。三星设备上的形状可绘制对象默认为黑色背景 - Toris

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