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

时间:2014-05-13 14:54:00

标签: java android xml textview drawable

我想显示一行数字,每行都打印在不同圆圈的中心。但是,集合的大小由用户输入决定。我所取得的成就是:

            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);

            }

它工作正常,但有一个问题。圆的半径始终是TextView的宽度或高度的一半。所以在上面的例子中,半径总是30,就像这张图片一样:pic1

如果TextView的宽度和高度不同(例如使用temptv.setWidth(75);temptv.setHeight(50);),那么我会得到椭圆形,如下图所示:pic2 http://i841.photobucket.com/albums/zz337/lilpengi/number2_zps767ec27f.png

无论我的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>

无论我在半径10,25,50处设置什么,结果都是一样的。我的代码出了什么问题?

1 个答案:

答案 0 :(得分:2)

使用标准的Android小部件似乎是不可能的,因为你在组件周围画了一个“椭圆形”。

椭圆可以变成圆形,因为圆是高度等于宽度的专业化。我的建议是重新实现计算组件大小的方式。只需通过覆盖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/