Android中的9个补丁按钮问题

时间:2017-08-07 00:36:52

标签: android nine-patch

我试图制作一个带有3d外观的自定义按钮样式,但我遇到了问题"按下"按钮的状态。该按钮可以正确地更改图像,但文本保持不变,这使得文本看起来实际上在按下时向上滑动。

expected vs reality

(添加红线以显示取消按钮的基线)

按钮视图的XML(cmdNegative是取消按钮,layDialogControl是按钮正上方的布局)

<Button
    android:id="@+id/cmdPositive"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/button_positive"
    android:text="Accept"
    android:layout_marginTop="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintTop_toBottomOf="@+id/layDialogControl"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@id/cmdNegative"
    app:layout_constraintBottom_toBottomOf="parent" />

按钮样式的XML(button_gray与button_green完全相同......好吧......灰色)

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

未压缩的9补丁:unpressed 9 patch

按下9补丁:pressed 9 patch

据我所知,设置右侧/底部的像素应该已经定义了&#34;内容区域&#34;并且文本应保留在内容视图中,buuuuuuut并非如此。任何关于我搞砸了的想法或指示都将不胜感激。

1 个答案:

答案 0 :(得分:0)

我认为你误解了9补丁图片。您定义的可伸展区域(顶部和左侧)将根据您的布局定义进行拉伸。定义的内容区域,右侧和底部部分将设置内容的限制,类似于指定保留内容的区域的矩形/正方形。单击时,您接受按钮(按下)的图像不会拉伸,因此无论您的图像是什么样子都显示出来。

此外,如果您的图片中包含文字,那么就没有内容区域。如果您的按钮图像已定义文本,则可以通过重新定义内容区域来解决问题。换句话说,在压缩状态下,您只需要替换具有正确内容区域区域的图像,使其看起来像是在点击时被拉伸但实际上只是另一个图像。

相关问题