Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.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 Layout_Android Widget - Fatal编程技术网

Android 围绕圆形动态排列按钮

Android 围绕圆形动态排列按钮,android,android-layout,android-widget,Android,Android Layout,Android Widget,我还是android新手,所以我并不完全熟悉所有的视图组件。 我正在努力使按钮围绕一个圆圈动态对齐 我试图实现的是将n个按钮(n个可以在创建时更改)添加到一个看起来像附加图像的视图中: 我希望避免使用absoluteLayout(但如果这是唯一的解决方法,我愿意接受建议)。 我已经计算出按钮的x/y位置(现在忽略按钮大小): int iNumberOfButtons=10; double DinRise=Math.PI*2/iNumberOfButtons, 悬挂=0, x=0, y=0; 对

我还是android新手,所以我并不完全熟悉所有的视图组件。 我正在努力使按钮围绕一个圆圈动态对齐

我试图实现的是将n个按钮(n个可以在创建时更改)添加到一个看起来像附加图像的视图中:

我希望避免使用absoluteLayout(但如果这是唯一的解决方法,我愿意接受建议)。 我已经计算出按钮的x/y位置(现在忽略按钮大小):

int iNumberOfButtons=10;
double DinRise=Math.PI*2/iNumberOfButtons,
悬挂=0,
x=0,
y=0;
对于(int i=0;i
我曾考虑在自定义视图中使用此代码,但据我所知,视图需要从ViewGroup中进行子类化,以使用addView方法,然后再次强调,只有absoluteLayout似乎允许设置x,y位置。。。我不知道如何实现这个特性


稍后我可能会在该视图中添加一些动画,因此如果可能的话,使用SurfaceView可能会很好,但这不是一个要求。

您可以使用RelativeLayout和to child view来代替绝对布局,设置顶部和左侧的边距。大概是这样的:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
params.leftMargin = x;
params.topMargin = y;

我想我找到了我试图实现的解决方案

我创建了自己的视图子类RelativeLayout。在onCreate()中,我设置了

这样就可以调用onDraw()。 然后我继续在onDraw()中:

intiheight=getHeight();
int-iWidth=getWidth();
int iNumberOfButtons=10;
double DinRise=Math.PI*2/iNumberOfButtons,
悬挂=0,
x=0,
y=0;
对于(int i=0;i

这给了我期望的结果,但是LayoutParams的初始化感觉(很可能)是错误的。有更好的方法吗?

下面的Apache 2.0许可项目可能有用:

这是个好主意,你的建议实现了我想要做的。为此,我将RelativeLayout子类化,并在onCreate中创建按钮。但此时,它们的LayoutParams(以及视图本身中的参数)为空。初始化它们的首选方式是什么?我忘了提到的另一个要求是圆应该位于视图的中心。我认为只需添加从getHeight()和getWidth()获取的值来偏移位置是很容易的,但这两种方法都返回0(getMeasuredWidth()和Height也是如此)。有什么办法可以解决这个问题吗?我在使用这种方法时遇到了另一个问题。onDraw()会被反复调用,这不是我在那里设置按钮时想要的。我想我可以用if和boolean来围绕初始化,但是有没有一种可以接受的方法来实现我想要的行为呢?(例如,在我的自定义视图中使用按钮,从中心以圆形排列,而不使用onDraw?)我想要按钮单击事件,但我无法找到。
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
params.leftMargin = x;
params.topMargin = y;
setWillNotDraw(false);
int iHeight = getHeight();
int iWidth = getWidth();
int iNumberOfButtons = 10;
double dIncrease = Math.PI * 2 / iNumberOfButtons,
       dAngle = 0,
       x = 0,
       y = 0;

  for( int i = 0; i < iNumberOfButtons; i++ )
  {
    x = 200 * Math.cos( dAngle ) + iWidth/2;
    y = 200 * Math.sin( dAngle ) + iHeight/2;
    dAngle += dIncrease;
    Button xButton = new Button(m_xContext);
    xButton.setAdjustViewBounds(true);
    xButton.setBackgroundResource(R.drawable.some_image);
    LayoutParams xParams = (RelativeLayout.LayoutParams)xButton.getLayoutParams();
    if( xParams == null )
    {
      xParams = new RelativeLayout.LayoutParams( xButton.getBackground().getIntrinsicWidth(), xButton.getBackground().getIntrinsicHeight() );
    }
    xParams.leftMargin = (int)x - ( xButton.getBackground().getIntrinsicWidth() / 2 ) ;
    xParams.topMargin =  (int)y - ( xButton.getBackground().getIntrinsicHeight() / 2 );
    addView( xButton, xParams );
  }