Как добавить еще один номер макета в мой уже созданный LinerLayout? - PullRequest
0 голосов
/ 11 января 2012

Вот мой макет Prent (main.xml)

<!-- ============================================================ -->
            <!--  All Employees are added here (added and More then one) -->
            <!-- ============================================================ -->
            <LinearLayout 
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/myLayout"
                android:orientation="vertical"
                android:layout_gravity="center_vertical"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#00000000">

                <!-- ++++++++ here extra layout is added programmatically -->


            </LinearLayout>

Вот еще один мой файл макета, который я хочу, как ребенок, скажем child.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout         
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout2"         
    android:layout_width="fill_parent"         
    android:layout_height="wrap_content"         
    android:layout_alignParentLeft="true"         
    android:layout_alignParentTop="true"         
    android:gravity="center_vertical"         
    android:orientation="horizontal" >          

    <Button             
        android:id="@+id/btn_left"             
        android:layout_width="100dp"             
        android:layout_height="wrap_content" 
        android:singleLine="false"   
        android:maxLines="2"         
        android:text="hello"/>
    <TextView             
        android:id="@+id/list_title"             
        android:layout_width="fill_parent"             
        android:layout_height="wrap_content"             
        android:layout_weight="0.5"             
        android:gravity="center_horizontal"             
        android:text="Photos"             
        android:textAppearance="?android:attr/textAppearanceLarge" />          
    <Button             
        android:id="@+id/btn_right"             
        android:layout_width="100dp"             
        android:layout_height="wrap_content"             
        android:text="Save" />     
</LinearLayout>

Теперь в моемОсновная деятельность Я вызываю main.xml и на основании условия цикла for хочу добавить макет child.xml и каждый раз, когда я хочу установить другое значение TextView child.xml

Так как это возможно?

Я сделал так:

private void doCalculationForMultipleEmployee() {
    singleEmployee.setVisibility(View.GONE);
    for (int i = 0; i<=tempEmployerList.size()-1; i++) {  
        View repeatedLayout = LayoutInflater.from(getApplicationContext()).inflate(R.layout.test);     
        ((TextView)repeatedLayout.findViewById(R.id.list_title)).setText("Employee"+i);     
        // customize repeatedLayout with other data     
        myLinearLayout.addChild(repeatedLayout);
    }
}

Но после этого я получил синтаксическую ошибку в .inflate и в .addChild

Так гдея ошибся?Пожалуйста, помогите мне с помощью некоторого кода, чтобы добавить его для цикла.

Ответы [ 3 ]

2 голосов
/ 11 января 2012

Просто создайте цикл, как показано ниже:

    LinearLayout parent=findViewById(R.id.myLayout);
    for(int i=0;i<list.size();i++)
    {
       LinearLayout llItem=(LinearLayout)LayoutInflater.createFromSource(R.layout.child, null);
       TextView txt= (TextView)llItem.findViewById(R.id.title);
       txt.setText(list.get(i));
       parent.add(llItem);
    }
1 голос
/ 11 января 2012

Раздуть представление, затем добавить к родителю

LinearLayout parent = (LinearLayout)findViewById(R.id.myLayout);

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Layout child = inflater.inflate(R.layout.myLayout2, null);
TextView title = (TextView)child.findViewById(R.id.list_title);
parent.addView(child);

Повторить столько раз, сколько необходимо, или использовать цикл

0 голосов
/ 11 января 2012

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

...