【问题标题】:Android complex shape buttonAndroid 复杂形状按钮
【发布时间】:2013-11-05 19:39:55
【问题描述】:

Android 开发者您好,

我的 UI 设计师想要一个复杂形状的按钮,但我不知道该怎么做,请帮助我。

这就是她想要的设计image

【问题讨论】:

  • 祝你好运.. 不容易 :) 你可以做的一件事:把整个事情当作图像,然后使用Touch Listener 并根据坐标确定按下了哪些按钮。标准按钮/布局可能在这里不起作用。
  • 尚未测试,但这可能有效:stackoverflow.com/questions/18074303/…

标签: android button user-interface shape


【解决方案1】:

有很多方法可以做到这一点,最简单的可能是为每个按钮创建一个 xml 选择器,该选择器将遵守您想要的状态(正常、按下、禁用等)。例如:

创建可绘制的 hex_button_one.xml

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

    <!-- Pressed -->
    <item android:state_pressed="true" >
       <bitmap android:antialias="true" 
               android:dither="true" 
               android:src="@drawable/myButtonPressed" />
    </item>

    <!-- normal -->
    <item>
        <bitmap android:antialias="true" 
               android:dither="true" 
               android:src="@drawable/myButtonNormal" />
    </item>
</selector>

然后你可以用你的布局xml文件来设置按钮的背景

 <Button android:id="@+id/hexButtonOne"
        android:layout_height="26dp"
        android:layout_width="26dp"
        android:layout_centerHorizontal="true"
        android:background="@drawable/hex_button_one"/>

这样做的主要问题是你会得到一个响应你触摸的矩形区域,如果你按照图像中的方式组织按钮,那么单击一个实际上可能会选择另一个。

您可以通过覆盖 Button 类的 onTouchEvent 来解决这个问题(这意味着您必须扩展它),如果它不在您希望它所在的区域,则返回 false。

总的来说,最好的——也是最完整的——方法是使用 onMeasure、onDraw 等方法扩展 View 类,以准确执行您想要的操作。但是,如果您是 Android 新手或者之前没有这样做过,我建议您先尝试我上面所说的内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 2016-11-02
    相关资源
    最近更新 更多