我想在android中创建一个Textviews循环,如下面标记的图像。我最终的目标是制作一个时钟,用手指向左或向右滑动数字。
我能够使用我收到的代码生成以下内容 Android align Image Buttons on a curve?
主要问题是代码基于绝对布局。我想要一些适用于所有屏幕尺寸的东西,所以像这样的东西没有绝对布局或任何其他如何做到这一点的建议将非常有帮助。
编码编辑了一点。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AbsoluteLayout al = new AbsoluteLayout(this);
setContentView(al);
double radius = 300;
double cx = 600, cy = 600;
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(600, 300, (int) x, (int) y);
textView.setLayoutParams(lp);
al.addView(textView);
}}
答案 0 :(得分:1)
不是设置半径,cx,cy(假设它们是中心坐标?)硬编码,而是通过您的absolutelayout getWidth()
和getHeight()
来获取它们。
答案 1 :(得分:1)
作为对我长期使用和喜爱的社区的贡献。 以下代码最初来自Android align Image Buttons on a curve?我根据我的理解编辑了以下内容,以便在RelativeLayout上工作。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout al = new RelativeLayout(this);
setContentView(al);
// Change the radius to change size of cirlce
double radius = 300;
// Change these to control the x and y position of the center of the circle
// For more screen friendly code changing this to margins might be more efficient
double cx = 600, cy = 600;
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));
//the 150 is the width of the actual text view and the 50 is the height
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(150,50);
//the margins control where each of textview is placed
lp.leftMargin = (int) x;
lp.topMargin = (int) y;
al.addView(textView, lp);
}
}