在一个活动中显示TextView的圆圈和内容

时间:2015-07-02 11:16:41

标签: java android

我编写了以下代码,我希望显示圆圈和 TextView 的内容,它具有圆圈区域。但是当我运行以下代码时,它只显示圆圈,它不能同时显示圆圈和 TextView 的内容。解决方案是什么?

Draw.java

public class Draw extends View {
Paint paint = new Paint();
Draw(Context context) {
    super(context);
}
public void onDraw(Canvas canvas) {
  paint.setColor(Color.BLUE);
    canvas.drawCircle(120,120,40,paint);
}
}

Cal.java

public class Cal extends View {
    Cal(Context context){
        super(context);
    }
    public double result;
    double parameter = (Math.pow(40,2)) * 3.14;
    public void cal(){
        result = Math.sqrt(parameter);

    }
}

MainActivity.java

public class MainActivity extends Activity{
    Draw draw;
    Cal cal;
    TextView textView;
    public void onCreate(Bundle s){
        super.onCreate(s);
        setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
        cal = new Cal(this);
        cal.cal();
        textView.setText(""+ cal.result);
draw = new Draw(this);
        setContentView(draw);
    }
}

2 个答案:

答案 0 :(得分:0)

textView.setText(""+ cal.result);更改此行,如下所示

textview.setBackground(cal.result);
textview.setText("your text goes here");

(OR)

使用<layer-list>绘制圆圈并将其设置为文本视图背景

太简单了
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="oval" >
            <solid android:color="#222222" />

            <size
                android:height="28dp"
                android:width="28dp" />
        </shape>
    </item>

</layer-list>

您必须在drawable中创建.xml文件

答案 1 :(得分:0)

enter image description here XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
</LinearLayout>

单次使用Activtiy

完成
public class MainActivity extends Activity {
Draw draw;
Cal cal;
TextView textView;
LinearLayout linearLayout;

public void onCreate(Bundle s) {
    super.onCreate(s);
    setContentView(R.layout.test1);

    linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
    cal = new Cal(this);
    cal.cal();

    textView = new TextView(getApplicationContext());
    textView.setText("" + cal.result);
    textView.setTextColor(Color.RED);

    draw = new Draw(this);
    linearLayout.addView(textView);
    linearLayout.addView(draw);
}

public class Draw extends View {
    Paint paint = new Paint();

    Draw(Context context) {
        super(context);
    }

    public void onDraw(Canvas canvas) {
        paint.setColor(Color.BLUE);
        canvas.drawCircle(120, 120, 40, paint);
    }
}

public class Cal extends View {
    Cal(Context context) {
        super(context);
    }

    public double result;
    double parameter = (Math.pow(40, 2)) * 3.14;

    public void cal() {
        result = Math.sqrt(parameter);

      }
   }
}