安卓圆形按钮

标签 android button

如何制作自定义圆形按钮? (只点击圆圈)

有什么方法可以用圆形png文件制作它吗?

我尝试使用 imageView 覆盖 onTouch 方法,但效果很差,因为 view.getWidth()、view.getHeight() 和 view.getTop... 方法效果很差

public boolean inCircle(MotionEvent e, int radius, int x, int y) {
    int dx = (int) (e.getX() - x);
    int dy = (int) (e.getY() - y);
    double d = Math.sqrt((dx * dx) + (dy * dy));
    if (d < radius)
        return true;
    return false;
}

谢谢。

最佳答案

非常简单。创建一个可绘制的自定义形状并将其设置为 View 的背景。示例:

drawable 中的 round_button_drawable.xml/

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

将此可绘制对象设置为任何 View 的背景。

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/round_button_drawable"
    android:text="btn"
/>

关于安卓圆形按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11792431/

相关文章:

android - 暗模式 : useColorScheme() always returns light on Android

android - 如果我们必须在 android 中使用 viewpager 创建多个页面,那么我们是否必须创建多个 fragment ?还有其他选择吗?

android - 从外部文件中读取 android URL

Android 确定单击哪个按钮来启动 Activity

swift - 如何在点击时在按钮切换中显示一张不同的图像?

javascript 两个按钮使用单一功能互相改变

android - 覆盖库的 dimens.xml 值

android - 暂停后 MediaPlayer 完成

css - 如何使按钮看起来像一个链接?

android - 在 ImageView 中加载的图像隐藏了按钮