【问题标题】:Android - repeat background and apply a white to transparent gradientAndroid - 重复背景并将白色应用于透明渐变
【发布时间】:2012-04-22 18:43:50
【问题描述】:

我想创建一个背景,其中包含水平和垂直重复的图像。在整个背景之上,我想应用从白色到透明的渐变,覆盖背景的一半。 现在,我想知道我是否可以用 style 做到这一点。这是因为我收到了这个警告:

"Possible overdraw: Root element paints background @drawable/background with a 
theme that also paints a background (inferred theme is @android:style/Theme)"

看起来背景应用了两次,一次是默认主题 (Theme),然后是我的布局、可绘制元素或其他。

所以,我的问题是:如何通过创建新主题或创建可绘制对象并禁用会被新主题透支的默认主题背景来做到这一点?

谢谢大家!

【问题讨论】:

    标签: android background gradient repeat


    【解决方案1】:

    假设您要为所有窗口设置背景,样式定义如下:

    <style name="MyTheme" parent="@android:style/Theme.Holo">
        <item name="android:windowBackground">@drawable/activity_background</item>
    </style>
    

    如果您想为特定视图执行此操作,通常有一个“android:background”属性必须设置为您的可绘制对象。

    要让位图在其上带有渐变,必须使用这样的图层列表来定义您的可绘制对象:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <item>
        <bitmap
            android:dither="true"
            android:src="@drawable/dialog_background_img"
            android:tileMode="repeat" >
        </bitmap>
    </item>
    <item>
       <shape>
          <gradient
            android:angle="90"
            android:endColor="#66ff0000"
            android:startColor="#5500ff00" />
       </shape>
    </item>
    
    </layer-list>
    

    【讨论】:

    • 我收到一个错误,应用程序崩溃:无效的可绘制标签渐变
    • 如果我删除渐变效果。渐变有什么问题?
    • 好的。我必须将它包含在一个形状元素中。无论如何,这行得通,我非常感谢你!
    • 抱歉忘记了形状元素。我把它放在一起没有测试。很高兴知道你修好了。
    猜你喜欢
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-21
    • 2013-03-22
    • 2013-04-03
    • 2021-12-30
    相关资源
    最近更新 更多