简体   繁体   English

Android复杂形状按钮

[英]Android complex shape button

Hello android developers, 您好,Android开发人员,

My UI designer wants a complex shape button, but I don't know how do that, please help me. 我的UI设计师想要一个复杂的形状按钮,但我不知道该怎么做,请帮助我。

This is the design she wants image 这是她想要形象的设计

There are many ways to do this, the easiest would probably be to create an xml selector for each button that will abide by the states you want (normal, pressed, disabled, etc.). 有很多方法可以做到这一点,最简单的方法可能是为每个按钮创建一个xml选择器,该选择器将遵守您想要的状态(正常,按下,禁用等)。 For example: 例如:

Create the drawable hex_button_one.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>

Then with your layout xml file you can set the background for the button 然后,使用布局xml文件,您可以设置按钮的背景

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

The main issue with this is you will get a rectangle area that responds to you touches, and if you have the buttons organized as they were in the image then clicking one may actually be selecting another. 这样做的主要问题是,您将获得一个矩形区域来响应您的触摸,如果您按图像中的按钮组织了按钮,则单击一个按钮实际上可能会选择另一个按钮。

You may be able to solve this issue with overriding the onTouchEvent of the Button class (meaning you will have to extend it) and return false if it isn't in the area you want it to be in. 您可以通过重写Button类的onTouchEvent(意味着您必须对其进行扩展)来解决此问题,如果它不在您希望放入的区域中,则返回false。

Overall, the best --and most complete-- way of doing this would be to extend the View class using the onMeasure, onDraw, etc. methods to perform exactly what you want. 总的来说,最好(也是最完整)的方法是使用onMeasure,onDraw等方法扩展View类,以准确执行所需的操作。 However if you are new to Android or haven't done this before I would recommend trying what I have said above first. 但是,如果您是Android的新手,或者还未开始使用此工具,建议您先尝试上述内容。

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

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