我在主菜单上有一系列按钮。而不是标准的并排,或者一个在另一个之上,我希望它们围绕一个半圆对齐。由于我无法将按钮拖放到设计师想要的位置,我想知道最好的方法吗?我可以在XML中执行此操作,还是最好以编程方式执行此操作?
答案 0 :(得分:3)
这是一个围绕圆圈绘制一系列TextView的示例。您应该能够根据自己的需要进行调整。
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
AbsoluteLayout al = new AbsoluteLayout(this);
setContentView(al);
double radius = 75;
double cx = 100, cy = 100;
for(double angle = 0; angle < 360; angle += 30) {
double radAngle = Math.toRadians(angle);
double x = (Math.cos(radAngle)) * radius + cx;
double y = (1 - Math.sin(radAngle)) * radius + cy;
TextView textView = new TextView(this);
textView.setText(Double.toString(angle));
AbsoluteLayout.LayoutParams lp = new AbsoluteLayout.LayoutParams(60, 30, (int) x, (int) y);
textView.setLayoutParams(lp);
al.addView(textView);
}
}