Android的复杂形状的按钮 [英] Android complex shape button

查看:156
本文介绍了Android的复杂形状的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好Android开发者,

Hello android developers,

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

My UI designer wants a complex shape button, but I don't know how do that, please help me.

这是她想要图片

推荐答案

有很多方法可以做到这一点,最简单的很可能会创建一个XML选择器为每个按钮将你想要的状态遵守(正常,pressed,残疾人等)。例如:

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.). For example:

创建绘制hex_button_one.xml

Create the drawable 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文件,你可以设置背景的按钮

Then with your layout xml file you can set the background for the button

 <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.

您可能能够与覆盖Button类的的onTouchEvent(这意味着你必须扩展它),并返回false,如果它不是在你希望它是在该地区的来解决这个问题。

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.

总体而言,这样做的最好的 - 并且最complete--方法是延长使用onMeasure,OnDraw中,等等方法来执行你想要什么View类。但是,如果你是新的Andr​​oid或之前,我会建议您尝试什么,我第一次上面已经说过没有这样做。

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. However if you are new to Android or haven't done this before I would recommend trying what I have said above first.

这篇关于Android的复杂形状的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆