Android的选择与背景图像和渐变 [英] Android selector with background image and gradient

查看:80
本文介绍了Android的选择与背景图像和渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有类似的帖子这是,但我找不到我在其中的任何答复。所以,我有这个可绘制的XML:

I know there are similar post to this but I couldn't find my answer in any of them. So, I have this drawable XML:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true">
    <bitmap
        android:src="@drawable/bm_btn_background"
        android:tileMode="repeat"
        android:gravity="center" />
</item>
<item android:state_enabled="true">
    <shape android:shape="rectangle">
        <gradient
            android:startColor="#a0e0b071"
            android:endColor="#a0a67637"
            android:angle="270" />
        <stroke
            android:width="1dp"
            android:color="#5c3708" />
        <corners
            android:radius="5dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
</item>
<item android:state_pressed="true" >
    <shape>
        <gradient
            android:startColor="#a0a67637"
            android:endColor="#a0e0b071"
            android:angle="270" />
        <stroke
            android:width="1dp"
            android:color="#5c3708" />
        <corners
            android:radius="5dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
</item>

我试图用重复的图像作为背景,并应用了渐变创建一个按钮。有了这个code我只看到了背景图片,而不是渐变也没有边框和圆角。此外,当我按一下按钮,它不会改变(渐变应该改变)。我不知道什么是不对的code?相反,如果一个选择器我用了一层名单,我得到了想要的结果,但它不会改变任何时候我preSS的按钮。感谢您的帮助!

I am trying to create a button with a repeated image as background and a gradient applied to it. With this code I only see the background image, not the gradient nor the border and the rounded corners. Also, when I click the button, it doesn't change (the gradient is supposed to change). I don't know what is wrong with this code? If instead of a selector I use a layer-list, I get the desired result but it doesn't change either when I press the button. Thanks for your help!

推荐答案

您$ C $下的选择是错误的,因为:

Your code for the selector is wrong because:

  • 您有两个要素相同的状态,并作为选择器遇到的第一个国家( state_enabled )的位图元素,它将停在那里,你的渐变将不会出现(这个你应该使用图层列表有作为项目位图和顶部的梯度)

  • You have two elements for the same state and as the selector encounters the first state(state_enabled) for the Bitmap element it will stop there and your gradient will never appear(for this you should use a layer-list that has as items the Bitmap and the gradient on top)

选择器会为了匹配状态。当你preSS的按钮 STATE_ pressed 将永远不会被激活,因为选择将首先匹配在 state_enabled 这是第一个元素(这个你应该将code为 STATE_ pressed 上面的state_enabled元素)。

The selector will match states in order. As you press the Button the state_pressed will never be activated because the selector will match first the state_enabled that is on the first element(for this you should move the code for the state_pressed above the state_enabled elements).

事实上,你应该只删除 state_enabled 并让位图 + 梯度是在按钮的默认值。贝娄是你的选择(我以为你只是想改变渐变的图像上(但图像会出现,即使在pressed状态,如果这不是想要的行为,只留下梯度为 STATE_ pressed )):

In fact you should just remove the state_enabled and let the Bitmap + gradient be the default value for the Button. Bellow is your selector(I assumed you only want to change gradient on the image(but the image should appear even in the pressed state, if this isn't the wanted behavior leave only the gradient for the state_pressed)):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <layer-list>
            <item>
                <bitmap android:gravity="center" android:src="@drawable/bm_btn_background" android:tileMode="repeat" />
            </item>
            <item>
                <shape>
                     <gradient android:angle="270" android:endColor="#a0e0b071" android:startColor="#a0a67637" />
                     <stroke android:width="1dp" android:color="#5c3708" />
                     <corners android:radius="5dp" />
                     <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
                </shape>
            </item>
        </layer-list>
    </item>

    <item android:state_enabled="true">
        <layer-list>
            <item>
                <bitmap android:gravity="center" android:src="@drawable/bm_btn_background" android:tileMode="repeat" />
            </item>
            <item>
                <shape android:shape="rectangle">
                    <gradient android:angle="270" android:endColor="#a0a67637" android:startColor="#a0e0b071" />
                    <stroke android:width="1dp" android:color="#5c3708" />
                    <corners android:radius="5dp" />
                    <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
                </shape>
            </item>
        </layer-list>
    </item>


</selector>

这篇关于Android的选择与背景图像和渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆