使用视图膨胀多次 [英] Use view to inflate multiple times
问题描述
我有一些问题,关于膨胀和相同的TextView重新使用。结果
它喜欢它试图一遍又一遍地覆盖相同的TextView或东西,它不能做到这一点?
I have a some problem regarding inflating and re-using the same TextView.
Its like its trying to overwrite the same textview over and over again or something and it cant do that?
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
Here is the 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(数字);
This is the error im getting and its on row: 51, which is:
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)
推荐答案
你从来没有真正加入任何浏览到您的活动。您正在创建新的TableRows和添加TextViews给他们,但你从来没有添加行到任何东西。假设你在 R.layout.days_monday_inflate
A TableLayout,你应该首先得到该视图( TableLayout布局=(TableLayout参考)mainLayout.findViewById (R.id.my_table_layout_id)
),然后每行添加到TableLayout:
You're never actually adding any Views to your Activity. You're creating new TableRows and adding TextViews to them, but you're never adding the rows to anything. Assuming you have a TableLayout in R.layout.days_monday_inflate
, you should first get a reference to that view (TableLayout layout = (TableLayout)mainLayout.findViewById(R.id.my_table_layout_id)
) and then add each row to that 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,我建议。
Although I would recommend setting up your layout fully in XML if at all possible, unless needed dynamically. Also, if you're only adding one TextView per row, you're better off just using a LinearLayout, I would suggest.
这篇关于使用视图膨胀多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!