【问题标题】:a circle of textViews in androidandroid中的一圈textViews
【发布时间】:2014-03-28 08:46:51
【问题描述】:

我想在 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);
    }}

【问题讨论】:

    标签: android layout textview geometry


    【解决方案1】:

    不要设置半径、cx、cy(假设它们是中心坐标?)硬编码,而是通过绝对布局的getWidth() 和getHeight() 获取它们。

    【讨论】:

    • 我的意思是我想摆脱绝对布局,因为它已被弃用。但是你可以用 realtivelayout 做到这一点吗?还是框架布局?
    • 你应该尝试使用RelativeLayout并绝对使用marginLeft和marginTop属性来定位元素
    • 所以可以说我希望 90 成为屏幕的中心,什么代码会以真实布局为中心
    • @ksparakis 将其 leftMargin 设置为布局的宽度/2 减去视图的宽度/2,topMargin = 布局的高度/2 - 视图的高度/2
    • 该行给我一个问题,无法将其完全转换为相对布局。有什么建议么。在 absolutelayout 中,它的输入是(宽度,高度,x,y),而相对我相信只是(x,y)。试图破译代码我不知道发生了什么。
    【解决方案2】:

    作为对我长期使用和喜爱的社区的贡献。 以下代码最初来自 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);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2016-05-20
      • 2012-06-21
      • 2011-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多