【问题标题】:Set radial gradient as window background将径向渐变设置为窗口背景
【发布时间】:2011-08-21 14:58:09
【问题描述】:

我有以下梯度_bg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:type="radial"
        android:gradientRadius="200"
        android:startColor="#666666"
        android:endColor="#AAAAAA"
        />
</shape>

我想将此渐变用作我的应用程序的android:windowBackground 设置。然后,该应用程序将在其顶部有一些带有透明背景的视图。

我尝试过使用以下方法直接应用此渐变:

android:windowBackground="@drawable/gradient_bg"
在我的应用程序清单中,然后将滚动视图背景设置为透明,但我只获得了默认的黑色背景。

我将如何实现这个结果?我特别不想将背景应用于滚动视图。

提前感谢您的帮助。

【问题讨论】:

    标签: android background window drawable gradient


    【解决方案1】:

    另一种方法可能是创建一个主题(在 values/styles.xml 中):

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="windowbg" parent="android:Theme">
            <item name="android:windowBackground">@drawable/rectangle</item>
        </style>
    </resources>
    

    现在您可以像这样将此主题添加到您的活动/应用程序中:

    android:theme="@style/windowbg"
    

    【讨论】:

    • 这个效果我用过,这个应该可以!
    • 您能否建议,我如何以编程方式定义这种“windowBackground”样式?
    【解决方案2】:

    尝试从形状制作位图。因此,请创建与您的形状相关的另一个 xml。一些例子:

    <?xml version="1.0" encoding="utf-8"?>
    <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
            android:src="@drawable/background"
            android:tileMode="repeat"/>
    

    【讨论】: