隐形/透明按钮在Android中像常规一样工作?

时间:2021-11-11 11:19:09

How do I get one area in the middle of a image clickable (not the whole image)? Tried with a button set to invisible and clickable but the button does not work. What are the alternatives to an invisible / transparent button that works like a regular?

如何在图像中间点击一个区域(不是整个图像)?尝试按钮设置为不可见和可点击但按钮不起作用。隐形/透明按钮有哪些替代方案可以像普通按钮一样工作?

I've also thought of a completely transparent and clickable PNG that should work but maybe not the best way?

我还想到了一个完全透明且可点击的PNG应该可以工作,但也许不是最好的方法吗?

4 个解决方案

#1


23  

Here you go:

干得好:

Button theButton = (Button)findViewById(R.id.theButton);
theButton.setVisibility(View.VISIBLE);
theButton.setBackgroundColor(Color.TRANSPARENT);

phoneButton.setOnClickListener(new OnClickListener()
{   
    @Override
    public void onClick(View v)
    {
        // DO STUFF
    }
});

#2


18  

android:background="@android:color/transparent"

#3


6  

You can try set transparent background to the button. But do not change buttons visibility, cause that prevents view from getting click events.

您可以尝试将透明背景设置为按钮。但是不要更改按钮可见性,因为这会阻止视图获取点击事件。

#4


2  

You can also use

你也可以使用

android:background = "@null"

#1


23  

Here you go:

干得好:

Button theButton = (Button)findViewById(R.id.theButton);
theButton.setVisibility(View.VISIBLE);
theButton.setBackgroundColor(Color.TRANSPARENT);

phoneButton.setOnClickListener(new OnClickListener()
{   
    @Override
    public void onClick(View v)
    {
        // DO STUFF
    }
});

#2


18  

android:background="@android:color/transparent"

#3


6  

You can try set transparent background to the button. But do not change buttons visibility, cause that prevents view from getting click events.

您可以尝试将透明背景设置为按钮。但是不要更改按钮可见性,因为这会阻止视图获取点击事件。

#4


2  

You can also use

你也可以使用

android:background = "@null"