11

我有一些关于膨胀和重新使用同一个 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)

4

4 回答 4

18

您实际上从未向您的活动添加任何视图。您正在创建新的 TableRows 并向其中添加 TextView,但您从未将行添加到任何内容中。假设您有一个 TableLayout in R.layout.days_monday_inflate,您应该首先获取对该视图 ( 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。

于 2012-07-18T21:41:44.360 回答
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);
于 2014-10-28T22:28:53.563 回答
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);
}
于 2012-07-18T21:15:36.670 回答
0

您正在尝试将单个 TextView 添加到多行,这就是导致您的错误的原因。您需要每次重新膨胀 layout_number 以获得新的 TextView。

我不确定您的 layout_number 文件中包含什么,但根据您在此处的内容,似乎您最好在循环 ala 中创建新的 TextView

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);
}
于 2012-07-18T21:13:10.433 回答