Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将不同数量的按钮放在一个圆圈中_Android_Android Button - Fatal编程技术网

Android 将不同数量的按钮放在一个圆圈中

Android 将不同数量的按钮放在一个圆圈中,android,android-button,Android,Android Button,我有一个带有几个圆形图像按钮的菜单。 目前,按钮按垂直顺序排列 直线布局。我想做的是 按钮围成一个圆圈。按钮的数量 是可变的: 如何放置可变数量的 沿着圆圈的按钮?稍后按钮将显示 可拖动,当按钮被拖动到 活动开始时圆圈的中心。下面是一个用于自定义圆圈布局的小代码snipplet: private RelativeLayout.LayoutParams modifyLayoutParams( RelativeLayout.LayoutParams lp, int

我有一个带有几个圆形图像按钮的菜单。 目前,按钮按垂直顺序排列 直线布局。我想做的是 按钮围成一个圆圈。按钮的数量 是可变的:

如何放置可变数量的 沿着圆圈的按钮?稍后按钮将显示 可拖动,当按钮被拖动到
活动开始时圆圈的中心。

下面是一个用于自定义圆圈布局的小代码snipplet:

    private RelativeLayout.LayoutParams modifyLayoutParams(
            RelativeLayout.LayoutParams lp, int degree) {
        /**
         * Determine in Quadrant or on Axis
         * Using Android convention. Right X-axis is degree 0, growing clockwise.
         * */
        degree = degree % 360;
        if (degree < 0) { // Make it positive
            degree += 360;
        }
        if (degree == 0) { // right x-axis. Right
            lp.addRule(RelativeLayout.RIGHT_OF, R.id.center);
            lp.addRule(RelativeLayout.CENTER_VERTICAL);
            lp.setMargins(radius, 0, 0, 0);
        } else if (degree > 0 && degree < 90) { // Quadrant IV. Lower Right
            lp.addRule(RelativeLayout.BELOW, R.id.center);
            lp.addRule(RelativeLayout.RIGHT_OF, R.id.center);
            // Determine margin. 
            lp.setMargins(getMarginX(degree), getMarginY(degree), 0, 0); 
        } else if (degree == 90) { // Bottom y-axis. Bottom
            lp.addRule(RelativeLayout.BELOW, R.id.center); // Above Center.
            lp.addRule(RelativeLayout.CENTER_IN_PARENT);
            lp.setMargins(0, radius, 0, 0);
        } else if (degree > 90 && degree < 180) { // Quadrant III. Lower Left
            lp.addRule(RelativeLayout.BELOW, R.id.center);
            lp.addRule(RelativeLayout.LEFT_OF, R.id.center);
            // Determine margin. 
            lp.setMargins(0, getMarginX(degree - 90), getMarginY(degree - 90), 0);
        } else if (degree == 180) { // Right x-axis. Left
            lp.addRule(RelativeLayout.LEFT_OF, R.id.center);
            lp.addRule(RelativeLayout.CENTER_VERTICAL);
            lp.setMargins(0, 0, radius, 0);
        } else if (degree > 180 && degree < 270) { // Quadrant II. Upper Left
            lp.addRule(RelativeLayout.ABOVE, R.id.center);
            lp.addRule(RelativeLayout.LEFT_OF, R.id.center);
            // Determine margin. 
            lp.setMargins(0, 0, getMarginX(degree - 180), getMarginY(degree - 180));
        } else if (degree == 270) { // Top y-axis. Top
            lp.addRule(RelativeLayout.ABOVE, R.id.center); // Above Center.
            lp.addRule(RelativeLayout.CENTER_IN_PARENT);
            lp.setMargins(0, 0, 0, radius);
        } else if (degree > 270 && degree < 360) { // Quadrant I. Upper Right
            lp.addRule(RelativeLayout.ABOVE, R.id.center);
            lp.addRule(RelativeLayout.RIGHT_OF, R.id.center);
            // Determine margin. 
            lp.setMargins(getMarginX(360 - degree), 0, 0, getMarginY(360 - degree));
        }
        return lp;
    }
    /** X offset i.e. adjacent length */
    private int getMarginX(int degree) {
        return Math.abs((int)(Math.cos(Math.toRadians(degree)) * radius));
    }
    /** Y offset i.e. opposite length */
    private int getMarginY(int degree) {
        return Math.abs((int)(Math.sin(Math.toRadians(degree)) * radius));
    }
}
这是一篇完整的文章。上面的代码基本上告诉我们如何定制布局并在那里放置控件

输出为:


不幸的是,您提供的链接中的描述不起作用。xml(activity_main.xml)有错误。但我可以接受这个想法;)我自己创建了一个包含5个元素的圆形布局