开发者

Android align Image Buttons on a curve?

I have a series of buttons on a main menu. Instead of the standard side by side, or one on top of the other, I'd like them to开发者_开发百科 be aligned around a semi-circle. Since I can't drag and drop the buttons to the place I'd like to in the designer, I was wondering the best way to do this? Can I do it in the XML, or would it be best to do it programatically?


Here's an example which plots a series of TextViews around a circle. You should be able to adapt it to suit your needs.

@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);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜