在我的RecylcerAdapter
中,我想複製TextView
。複製視圖時出錯:指定的子項已有父項
我有一個LinearLayout
其中包含一個TexView
。我只想在LinearLayout
內部動態複製TextView
,以便我在LinearLayout
內有10 TextView
。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tags_ll"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:gravity="right">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tags"
android:layout_gravity="center_vertical"
android:gravity="center"
android:text="عمومی"
android:layout_margin="5dp"
android:padding="5dp"
android:background="@drawable/border_with_background"
android:textSize="12dp" />
</LinearLayout>
MyViewHolder在RecyclerView:
public MyViewHolder(View view) {
super(view);
tags = (TextView) view.findViewById(R.id.tags);
tags_ll = (LinearLayout) view.findViewById(R.id.tags_ll);
for (int i = 0; i < 10; i++) {
TextView rowTextView = new TextView(view.getContext());
//Clone the new textview, get all the properties of the existing textview
rowTextView = tags;
rowTextView.setText("This is row #" + i);
tags_ll.addView(rowTextView);
}
}
我得到以下錯誤:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
我想現有的TextView的所有屬性將被複制到新創建的TextViews。
這應該是什麼? 'rowTextView = tags;' –
我想將已經存在的textview複製到這個新的文本視圖中。我希望將現有textview的所有屬性複製到新的textview中。 – SMahdiS
這是java。這不是你如何從一個對象複製屬性到另一個對象 –