【发布时间】:2014-05-13 09:23:30
【问题描述】:
我在一个包含这样的 TextView 的 XML 中创建了一个 RelativeLayout:
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/button1"
android:layout_marginTop="28dp"
android:background="@drawable/rounded_edges"
android:textSize="25sp" />
以便在文本下方绘制一个圆圈。 rounded_edges.xml 定义如下:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="10dip"/>
<stroke android:color="#c00000" android:width="2dip"/>
<solid android:color="#ffffff"/>
</shape>
然后我需要更多的文本视图,具体取决于用户输入的数字。每个 textView 都会在下方显示一个带有圆圈的数字。我成功地在 Java 代码中以编程方式创建了 textViews,如下所示,除了圆圈:
final TextView textView5 = (TextView) findViewById(R.id.textView5);
// size stores the number entered by user
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(50);
// size of Text
temptv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);
temptv.setLayoutParams(lp);
myLayout.addView(temptv, lp);
answer = "<font color='#8000ff'>" + mynum[i] + "</font>";
answer = answer + " ";
// Update the label with our dynamic answer
temptv.setText(Html.fromHtml(answer));
tv[i] = temptv;
tv[i].setId(i);
}
setContentView(myLayout);
有没有办法将android:background="@drawable/rounded_edges" 转换为Java 代码,以便我仍然可以使用rounded_edges.xml 来绘制圆圈?还是我必须在 Java 中完全从头开始绘制?
【问题讨论】:
标签: java android xml drawing android-relativelayout