如何在android中为按钮实现非矩形形状

时间:2013-10-20 08:26:50

标签: android

嗨,我必须实现这种布局。它有这种布局。 inactive buttons

我可以尝试将图标用作图像按钮,但按钮的活动状态有点像这个!

one button has been pressed

我该如何处理?

1 个答案:

答案 0 :(得分:1)

您应该使用selector,如下所示:

  1. 为按钮状态准备2张图像,并将其放入res/drawable文件夹。

    button_normal_green.png - 默认图片按钮。

    button_pressed_yellow.png - 按下按钮时显示。

  2. 现在,在“res / drawable /”文件夹中创建一个新的XML文件,无论您想要什么名称,在这种情况下,我们只需将名称命名为“new_button.xml”。该文件定义了哪个按钮状态属于哪个图像。

    <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
         <item android:drawable="@drawable/button_pressed_yellow" android:state_pressed="true" />
    
         <item android:drawable="@drawable/button_normal_green" />
      </selector>
    
  3. 3.将背景设置为按钮

    <ImageButton
        android:id="@+id/imageButtonSelector"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/new_button" />
    

    查看Complete Example

相关问题