简体   繁体   English

Android:TextView中的圆半径始终由TextView宽度和高度修复

[英]Android: Radius of Circle in TextView Always Fixed by TextView Width and Height

I'd like to display a row of numbers, each printed at the center of a different circle. 我想显示一行数字,每行都打印在不同圆圈的中心。 However, the size of the set is determined by user input. 但是,集合的大小由用户输入决定。 What I have achieved is this: 我所取得的成就是:

            RelativeLayout myLayout =  (RelativeLayout)findViewById(R.id.layout1);

            // Creating a new TextView
            TextView[] tv = new TextView[size+1];
            TextView temptv;

            for (i = 1; i <= size; i++) {
                temptv = new TextView(MainActivity.this);
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

                if (i > 1) {
                    lp.addRule(RelativeLayout.BELOW, R.id.textView5);
                    lp.addRule(RelativeLayout.RIGHT_OF, tv[i-1].getId());
                }
                else {
                    lp.addRule(RelativeLayout.BELOW, R.id.textView5);
                    lp.addRule(RelativeLayout.ALIGN_LEFT, R.id.textView5);
                }

                // width of Text
                temptv.setWidth(60);
                temptv.setHeight(60);
                // size of Text
                temptv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);
                temptv.setGravity(Gravity.CENTER|Gravity.CENTER_VERTICAL);
                temptv.setLayoutParams(lp);
                myLayout.addView(temptv, lp);

                answer = "<font color='#000000'>" + mynum[i] + "</font>";

                // Update the label with our dynamic answer
                temptv.setText(Html.fromHtml(answer));

                temptv.setBackgroundResource(R.drawable.circle1);

                tv[i] = temptv;
                tv[i].setId(i);

            }

It works fine, but there is a problem. 它工作正常,但有一个问题。 The radius of the circles is always half the width or height of the TextView. 圆的半径始终是TextView的宽度或高度的一半。 So in the above case, the radius is always 30, just like this picture: 所以在上面的例子中,半径总是30,就像这张图片一样: PIC1

If the width and height of the TextView are different (eg using temptv.setWidth(75); and temptv.setHeight(50); ), then I get ovals, like this picture: pic2 http://i841.photobucket.com/albums/zz337/lilpengi/number2_zps767ec27f.png 如果TextView的宽度和高度不同(例如使用temptv.setWidth(75);temptv.setHeight(50); ),那么我得到椭圆形,如下图所示: pic2 http://i841.photobucket.com/相册/ zz337 / lilpengi / number2_zps767ec27f.png

irrespective of the shape settings in my circle1.xml: 无论我的circle1.xml中的形状设置如何:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
     android:shape="oval">
<corners android:radius="25dip"/>
<stroke android:color="#c00000" android:width="5dip"/>
<solid android:color="#ffffff"/>
</shape>

Whatever I set at the radius, 10, 25, 50, the result is the same. 无论我在半径10,25,50处设置什么,结果都是一样的。 What is going wrong with my code? 我的代码出了什么问题?

With the standard Android widgets it's seems to be impossible, due you are drawing a "oval" around the component. 使用标准的Android小部件似乎是不可能的,因为你在组件周围画了一个“椭圆形”。

An oval could become a circle as a circle is an specialization where height is equal to width. 椭圆形可以变成圆形,因为圆形是高度等于宽度的特化。 My advice is to reimplement the way of component size is computed. 我的建议是重新实现计算组件大小的方式。 Just square the layout always by overriding onMeasure (here is the more simple way to do, please click on the link in the bottom): 只需通过覆盖onMeasure来平衡布局(这是更简单的方法,请点击底部的链接):

class SquareTextView extends TextView{

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int size = 0;
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();

        if (width > height) {
            size = height;
        } else {
            size = width;
        }
        setMeasuredDimension(size, size);
    }
}

http://www.jayway.com/2012/12/12/creating-custom-android-views-part-4-measuring-and-how-to-force-a-view-to-be-square/ http://www.jayway.com/2012/12/12/creating-custom-android-views-part-4-measuring-and-how-to-force-a-view-to-be-square/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM