繁体   English   中英

由png和overlay组成的XML drawable

[英]XML drawable composed of png and overlay

我有一个png按钮,启用,非按下。 当用户点击按钮时,我只想让png变暗。 我需要这样的东西:

  <selector xmlns:android="http://schemas.android.com/apk/res/android" >
  //normal button with background my_button.png
        <item 
            android:state_enabled="true" 
            android:drawable="@drawable/my_button"   //my_button.png
            />
  //pressed button with background my_button.png overlayed by 50% black
        <item 
            android:state_pressed="true"
            android:state_enabled="true"    
            >
            <RelativeLayout 
               android:layout_width="wrap_content"
               android:layout_height="wrap_content">

              <bitmap  android:src="@drawable/my_button"/>
              <color android:color="#00000088"/>
            </RelativeLayout>
        </item>      
    </selector>

有什么方法可以做到这一点? 或者我必须有另一个png文件吗?

在my_button_bg.xml中:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/button_normal"/>
</selector>

button_normal是一个png

button_pressed是一个xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/button_normal"/>
    <item android:drawable="@color/btn_bg_pressed_mask"/>
</layer-list>

其中btn_bg_pressed_mask是一种颜色:

<color name="btn_bg_pressed_mask">#19000000</color>

这应该工作

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:state_enabled="true"
        android:drawable="@drawable/my_button" />
    <item>
        <selector>
            <item
                android:state_pressed="true"
                android:state_enabled="true">
                <color android:color="#00000088" />
            </item>
        </selector>
    </item>

</layer-list>

选择器XML中项目的顺序有所不同。 第一场比赛是将要展示的内容。 正如你在marmor的回答中看到的那样,按钮的正常状态列在最后。

另外要记住的是,如果使用9补丁图像(.9.png),颜色将仅应用于内容区域。 因此,如果您希望将颜色覆盖在整个图像上,请确保将整个图像标记为内容区域。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM