在自定义的三角形按钮边界 [英] Bounds in custom triangle button

查看:381
本文介绍了在自定义的三角形按钮边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个自定义按钮,如图所示的形象。
我的问题是,边界仍然clickble。
有什么办法来包装的三角形。

I created a custom button as shown in the image. My problem is that the bounds still clickble. Is there any way to wrap the triangle.

自定义按钮

我的形状xml文件:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
    <rotate
        android:fromDegrees="45"
        android:toDegrees="45"
        android:pivotX="-10%"
        android:pivotY="87%" >
        <shape
            android:shape="rectangle" >
            <stroke  android:color="@color/transparent" android:width="30dp"/>
            <solid 
             android:color="@android:color/black" />

        </shape>
    </rotate>
</item>
</layer-list>

按钮:

    <LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:weightSum="1">

    <Button
        android:layout_width="114dp"
        android:layout_height="match_parent"
        android:background="@drawable/triangle"
        android:id="@+id/button"/>

    </LinearLayout>

我也试过帆布方法,并得到了同样的问题。

I also tried canvas approach and get the same problem.

推荐答案

对不起,我迟到的答复。我不知道你已经找到解决办法。
因为的android:pivotX = - 10%,你的黑三角与顶边的距离

Sorry for my late reply. I don't know you have found the solution yet. Because android:pivotX="-10%" ,your black triangle have distance with the top edge.

您可以删除的android:pivotX,机器人:pivotY ,并添加的android:layout_marginBottom = - 50dp在你的按钮是这样的:

You can remove android:pivotX, android: pivotY, and add android:layout_marginBottom="-50dp"in your button like this:

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:weightSum="1"
android:layout_marginBottom="-50dp"
>

<Button
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="@drawable/triangle"
    android:id="@+id/button"
    />
</LinearLayout>

现在你的黑色三角形的顶边没有距离。但它并没有真正包裹的三角形。如果你想换行的三角形,有一些解决方案(这并不容易):

Now your black triangle has no distance with the top edge. But it doesn't really wrap the triangle. If you want to wrap the triangle, there are some solution (It's Not Easy):

您可以覆盖在CustomView /的CustomButton的OnTouch事件的方法。

You can override the OnTouch Event method in a CustomView / CustomButton.

在里面你有MotionEvent是你可以检查是否触摸是你的三角形内(有一些数学的帮助:P)

Inside you have the MotionEvent were you can check if the Touch was inside your Triangle (with the help of some mathematics :P)

或者你也可以做同样的:

Or you can do the same:

Android的自定义形状按钮

这篇关于在自定义的三角形按钮边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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