我有一些关于充气和重复使用相同TextView的问题。
它就像试图一遍又一遍地覆盖相同的文本视图一样,它不能这样做吗?
LayoutInflater inflater = LayoutInflater.from(this);
View mainlayout = inflater.inflate(R.layout.days_monday_inflate, null);
View layout_number = inflater.inflate(R.layout.inflate_number, null);
for (int i = 0; i < 10; i++) {
row = new TableRow(this);
number = (TextView) layout_number.findViewById(R.id.Number);
number.setTag(i);
number.setText(Integer.toString(i));
row.addView(number);
}
setContentView(mainlayout);
这是inflate_number.xml
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Number"
android:layout_width="3dp"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
android:text="1" />
这是我得到的错误及其在行上:51,这是:
row.addView(number);
07-18 20:54:25.124: E/AndroidRuntime(1166): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
07-18 20:54:25.124: E/AndroidRuntime(1166): at android.view.ViewGroup.addViewInner(ViewGroup.java:1970)
07-18 20:54:25.124: E/AndroidRuntime(1166): at android.view.ViewGroup.addView(ViewGroup.java:1865)
07-18 20:54:25.124: E/AndroidRuntime(1166): at android.view.ViewGroup.addView(ViewGroup.java:1822)
07-18 20:54:25.124: E/AndroidRuntime(1166): at android.view.ViewGroup.addView(ViewGroup.java:1802)
07-18 20:54:25.124: E/AndroidRuntime(1166): at com.trainingschedule.days.Monday.onCreate(Monday.java:50)
07-18 20:54:25.124: E/AndroidRuntime(1166): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-18 20:54:25.124: E/AndroidRuntime(1166): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
答案 0 :(得分:18)
您实际上从未向您的活动添加任何视图。您正在创建新的TableRows并向其添加TextView,但您永远不会将行添加到任何内容。假设您在R.layout.days_monday_inflate
中有一个TableLayout,您应首先获得对该视图的引用(TableLayout layout = (TableLayout)mainLayout.findViewById(R.id.my_table_layout_id)
),然后将每一行添加到该TableLayout:
TableLayout layout = (TableLayout)mainLayout.findViewById(R.id.my_table_layout_id);
for(int i = 0; i < 10; i++) {
row = new TableRow(this);
View layout_number = inflater.inflate(R.layout.inflate_number, layout, false);
TextView number = (TextView) layout_number.findViewById(R.id.Number);
number.setTag(i);
number.setText(Integer.toString(i));
row.addView(number);
layout.addView(row);
}
虽然我建议尽可能使用XML完全设置布局,除非动态需要。另外,如果你每行只添加一个TextView,我建议你最好只使用一个LinearLayout。
答案 1 :(得分:2)
在我的情况下,setTag(i)不起作用,在扩充布局之后但在将其添加到父视图之前更改了ID是有效的:
LayoutInflater inflater = LayoutInflater.from(this);
View mainlayout = inflater.inflate(R.layout.days_monday_inflate, null);
View layout_number = inflater.inflate(R.layout.inflate_number, null);
for (int i = 0; i < 10; i++) {
row = new TableRow(this);
number = (TextView) layout_number.findViewById(R.id.Number);
number.setId(i); //THIS IS THE KEY STEP
number.setText(Integer.toString(i));
row.addView(number);
}
setContentView(mainlayout);
答案 2 :(得分:0)
您尝试将单个TextView添加到多行,这就是导致错误的原因。每次都需要重新填充layout_number以获得新的TextView。
我不确定你的layout_number文件中包含了什么,但是基于你在这里的内容,似乎你可以更好地在你的循环中创建新的TextViews ala
for (int i = 0; i < 10; i++) {
row = new TableRow(this);
number = new TextView(this);
number.setTag(i);
number.setText(Integer.toString(i));
row.addView(number);
}
答案 3 :(得分:0)
试试这个:
View mainlayout = inflater.inflate(R.layout.days_monday_inflate, null);
for (int i = 0; i < 10; i++) {
row = new TableRow(this);
View layout_number = inflater.inflate(R.layout.inflate_number, null);
TextView number = (TextView) layout_number.findViewById(R.id.Number);
number.setTag(i);
number.setText(Integer.toString(i));
row.addView(number);
}