Android的自定义形状按钮自定义、形状、按钮、Android

2023-09-12 22:08:08 作者:归字谣

我怎样才能使形机器人可点击视图或按钮自定义?

当我点击,我要避免接触上的空白区域。

请帮忙。谢谢。

解决方案

有趣的问题。我尝试了一些解决方案,这就是我发现有你想要达到的目的相同的结果。下面的解决方案可以解决两个问题:

在自定义形状您presented它 在顶部右侧的按钮不应该点击

所以,这是3个步骤的解决方案:

步骤1

创建两个形状。

首先简单的矩形形状的按钮: shape_button_beer.xml

 < XML版本=1.0编码=UTF-8&GT?;
<形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

    <梯度
        机器人:角=90
        机器人:endColor =#C5D9F4
        机器人:startColor =#DCE5FD/>

    <角落
        机器人:bottomLeftRadius =5DP
        机器人:bottomRightRadius =5DP
        机器人:topLeftRadius =5DP>
    < /角落>

< /形状>
 
android button自定义样式详解,Android自定义格式显示Button的布局思路

第二个形状被用作掩码右上侧的按钮: shape_button_beer_mask.xml 。这是简单的圆形,黑色纯色。

 < XML版本=1.0编码=UTF-8&GT?;
<形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:形状=椭圆形>

    [固体机器人:颜色=#000000/>

< /形状>
 

步骤2

在主布局明年的方法添加按钮:

RelativeLayout的是这样的自定义按钮的容器 在第一的LinearLayout是蓝色的按钮,啤酒图标和文本里面 在二ImageView的是蓝色的按钮上方的面具。这里来使坏: 在利润率为负设定在正确的地方面膜 我们定义ID是在点击能够覆盖(参见步骤3) 机器人:soundEffectsEnabled =假 - 这样用户就不会觉得他$ pssed的东西p $

中的XML:

 <! - 自定义按钮 - >
    < RelativeLayout的
        机器人:layout_width =120dp
        机器人:layout_height =80dp>

        <的LinearLayout
            机器人:ID =@ + ID / custom_buttom
            机器人:layout_width =100dp
            机器人:layout_height =100dp
            机器人:背景=@可绘制/ shape_button_beer>

            <! - 啤酒图标,和所有其他的东西 - >

            < ImageView的
                机器人:layout_width =40dp
                机器人:layout_height =40dp
                机器人:layout_marginLeft =5DP
                机器人:layout_marginTop =15dp
                机器人:SRC =@可绘制/ beer_icon/>
        < / LinearLayout中>

        < ImageView的
            机器人:ID =@ + ID / do_nothing
            机器人:layout_width =120dp
            机器人:layout_height =100dp
            机器人:layout_alignParentRight =真
            机器人:layout_alignParentTop =真
            机器人:layout_marginRight = -  50dp
            机器人:layout_marginTop = -  50dp
            机器人:背景=@可绘制/ shape_button_beer_mask
            机器人:soundEffectsEnabled =假>
        < / ImageView的>
    < / RelativeLayout的>
    <  - !结束自定义按钮 - >
 

步骤3

在您的主要活动中,您在点击事件都定义:按钮和​​掩码如下:

 的LinearLayout的CustomButton =(的LinearLayout)findViewById(R.id.custom_buttom);
customButton.setOnClickListener(新View.OnClickListener()
{
    @覆盖
    公共无效的onClick(查看arg0中)
    {
        Toast.makeText(getApplicationContext(),点击,Toast.LENGTH_SHORT).show();
    }
});

//面膜上点击会做什么
ImageView的doNothing =(ImageView的)findViewById(R.id.do_nothing);
doNothing.setOnClickListener(新View.OnClickListener()
{
    @覆盖
    公共无效的onClick(查看arg0中)
    {
        // 没做什么
    }
});
 

就是这样。我知道这是不是一个完美的解决方案,但在你的描述使用情况下,它可能会有所帮助。 我已经测试它在我的手机,这是怎么回事,当你点击蓝色区域,什么都不会发生在其他领域的样子:

希望这有助于以某种方式:)

How can i make a custom shaped clickable view or button in Android?

When I click , I want to avoid touching on an empty area .

please help. Thank you.

解决方案

Interesting question. I tried some solutions and this is what I found that has the same result of what you are trying to achieve. The solution below resolves 2 problems:

Custom shape as you presented it The top right side of the button shouldn't be clickable

So this is the solution in 3 steps:

Step 1

Create two shapes.

First simple rectangle shape for the button: shape_button_beer.xml

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

    <gradient
        android:angle="90"
        android:endColor="#C5D9F4"
        android:startColor="#DCE5FD" />

    <corners
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"
        android:topLeftRadius="5dp" >
    </corners>

</shape>

Second shape is used as mask for the top right side of the button: shape_button_beer_mask.xml. It is simple circle with black solid color.

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

    <solid android:color="#000000" />

</shape>

Step 2

In your main layout add the button by next approach:

RelativeLayout is the container of this custom button First LinearLayout is the blue button with beer icon and text inside Second ImageView is the mask above the blue button. And here comes dirty trick:

Margins are negative to set the mask in the right place We define id to be able override on click (see step 3) android:soundEffectsEnabled="false" - such that user will not feel that he pressed on something.

The XML:

    <!-- Custom Button -->
    <RelativeLayout
        android:layout_width="120dp"
        android:layout_height="80dp" >

        <LinearLayout
            android:id="@+id/custom_buttom"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@drawable/shape_button_beer" >

            <!-- Beer icon and all other stuff -->

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="15dp"
                android:src="@drawable/beer_icon" />
        </LinearLayout>

        <ImageView
            android:id="@+id/do_nothing"
            android:layout_width="120dp"
            android:layout_height="100dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="-50dp"
            android:layout_marginTop="-50dp"
            android:background="@drawable/shape_button_beer_mask"
            android:soundEffectsEnabled="false" >
        </ImageView>
    </RelativeLayout>
    <!-- End Custom Button -->

Step 3

In your main activity you define on click events for both: button and the mask as follow:

LinearLayout customButton = (LinearLayout) findViewById(R.id.custom_buttom);
customButton.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View arg0)
    {
        Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();
    }
});

// Mask on click will do nothing
ImageView doNothing = (ImageView) findViewById(R.id.do_nothing);
doNothing.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View arg0)
    {
        // DO NOTHING
    }
});

That's it. I know that is not a perfect solution but in your described use case it could help. I have tested it on my mobile and this is how it looks when you click on the blue area and nothing will happen on other areas:

Hope it helped somehow :)

 
精彩推荐
图片推荐