【问题标题】:Button raised effect not working when background image attached附加背景图像时按钮凸起效果不起作用
【发布时间】:2016-04-19 11:25:17
【问题描述】:

我正在创建一个简单的按钮,如下所示:

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/edSearch"
    android:id="@+id/btnSearch"
    android:layout_marginTop="30dp"
    android:drawableTop="@drawable/ic_action_search"
    android:textSize="16sp"
    android:text="Search"
    android:background="@color/md_green_500"
    android:textColor="#fff"
    />

现在,当我尝试在背景中添加任何图像时,例如:android:background="@drawable/img123" 然后按钮提升效果不起作用。有什么办法可以用背景图片实现提升效果?

【问题讨论】:

    标签: android button


    【解决方案1】:

    您可以为此创建一个可绘制的选择器。您需要为此提供 3 个不同的图像,以便按钮可以根据状态切换这些图像。

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="false"
        android:state_pressed="false"
        android:drawable="@drawable/image_normal" />
    <item android:state_focused="false"
        android:state_pressed="true" 
        android:drawable="@drawable/image_pressed" />
    <item android:state_focused="true"
        android:state_pressed="false" 
        android:drawable="@drawable/image_focused" />
    </selector>
    

    尽管如此,您也可以使用ImageButton,它将您的图像保持在按钮的中心,并且效果将根据操作系统的默认设置。

    【讨论】:

    • 是的,这是已知的方式。但是为什么在按钮上附加图像时我不能有凸起的按钮效果?
    • 这是因为按钮有一个默认的drawable。在后台应用图像时,您会覆盖该可绘制对象,因此现在您需要使用自定义可绘制对象或图像按钮来管理状态。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-29
    • 1970-01-01
    • 2011-09-19
    • 2023-03-03
    相关资源
    最近更新 更多