2016-06-12 1 views
0

Я пытаюсь добавить завышенный вид внутри контейнера LinearLayout. Однако я получаю, что у ребенка уже есть родительская проблема. Ниже приведен мой xml-файл с контейнером несколько раз. activity_main.xmlКак добавить раздутый просмотр несколько раз к линейной компоновке?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin"> 

    <LinearLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:background="@android:color/black" 
     ></LinearLayout> 
</LinearLayout> 

item_button.xml является XML, что я хочу, чтобы раздуть в контейнер.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 
    android:background="@color/purple" 
    android:orientation="vertical"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="25sp" 
     android:text="Button 1" 
     android:padding="20dp" 
     android:background="@color/colorAccent" 
     /> 

</LinearLayout> 

Ниже приведен код java внутри метода onCreate. Я хочу добавить несколько раз childView к контейнеру .:

 View childView; 
     LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     childView = inflater.inflate(R.layout.item_button, null); 
     container.addView(childView); 
     container.addView(childView); 

Однако добавление ребенка Mulitple раз к представлению дает следующие ошибки:

The specified child already has a parent. You must call removeView() 
on the child's parent first. 

ответ

2

Это происходит потому, что вы добавляете один и тот же вид мультипликатор раз, чтобы достичь того, чего вы хотите достичь, вам нужно только добавить представление один раз, и, следовательно, вам нужно создать новое представление каждый раз, когда вы хотите добавить представление в макет.

давайте предположим, что вы хотите добавить его в два раза:

View childView1, childView2; 
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
childView1 = inflater.inflate(R.layout.item_button, null); 
childView2 = inflater.inflate(R.layout.item_button, null); 
container.addView(childView1); 
container.addView(childView2); 

ИЛИ, если вы хотите, чтобы добавить мнение еще много раз:

View v1, v2, v3; 
View[] childViews = new View[]{v1,v2,v3}; 
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
for(int v = 0; v < childViews.length; v++){ 
    childViews[v] = inflater.inflate(R.layout.item_button, null); 
    container.addView(childViews[v]); 
} 
+0

Спасибо Muhammed. – Joyson

+0

Вы так приветствуетесь Joyson –

1

Как надутый вид уже был добавлен родительский макет, вам нужно раздувать его каждый раз, прежде чем добавлять его. Ваш код должен быть что-то вроде этого:

View childView; 
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
for (int i = 0; i < count; i++) { 
    childView = inflater.inflate(R.layout.item_button, null); 
    container.addView(childView); 
} 

count, где, очевидно, сколько раз вы хотите добавить item_button макет.

+0

Спасибо Rehan. Он решил проблему. – Joyson

Смежные вопросы