Как программно добавить несколько LinearLayout в одно представление, а затем добавить в ViewFlipper?

Надеюсь, заголовок вопроса дает вам хорошее описание проблемы.

I хотите создать этот XML, но программно (пожалуйста, не предлагайте не делать это программно ^ _ ^)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical"
android:padding="10dip" 
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView   android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dip"
            android:text="Messages:"/>

<EditText   android:id="@+id/messageHistory"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="false"   
            android:layout_weight="1"
            android:editable="false"
            android:gravity="top"
            android:scrollbars="vertical"
            android:scrollbarSize="10px"
            /> 

<LinearLayout android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_weight="4">

        <EditText   android:id="@+id/message"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:gravity="top"
                    android:layout_weight="1"
                    />

        <Button android:id="@+id/sendMessageButton"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="4"
                android:text="Send"/>

</LinearLayout>

Как видите, есть два LinearLayout, одно встроено в другое. Мне нужно воспроизвести это, а затем добавить во ViewFlipper.

Вот что у меня есть:

     LinearLayout l1 = new LinearLayout(this);
     LinearLayout l2 = new LinearLayout(this);       
     Button btn=new Button(this);
     EditText messageHistory = new EditText(this);
     EditText newMessage = new EditText(this);
     TextView windowTitle = new TextView(this);     
     btn.setText("Send");
     btn.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 4f));

     btn.setOnClickListener(new OnClickListener() {         
        @Override
        public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "Clicked send in child index: "+ flipper.getDisplayedChild(), Toast.LENGTH_SHORT).show();
            }           
    });

    windowTitle.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    windowTitle.setPadding(0, 5, 0, 10);
    windowTitle.setText("Chat with: ");

    messageHistory.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    messageHistory.setGravity(Gravity.TOP);
    messageHistory.setMovementMethod(new ScrollingMovementMethod());
    messageHistory.setClickable(false);
    messageHistory.setFocusable(false);     
    messageHistory.setPadding(0, 0, 0, 5);


    newMessage.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f));
    newMessage.setGravity(Gravity.TOP);
    newMessage.setMovementMethod(new ScrollingMovementMethod());
    newMessage.requestFocus();      

    l1.setOrientation(LinearLayout.VERTICAL);
    l1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    l2.setOrientation(LinearLayout.HORIZONTAL);
    l2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    l1.addView(windowTitle);
    l1.addView(messageHistory);
    l2.addView(newMessage);
    l2.addView(btn);
    l1.addView(l2);
    flipper.addView(l1);

Где flipper определяется как:

ViewFlipper flipper = (ViewFlipper) findViewById(R.id.viewflip);

Без проблем, я могу видеть «l1» в окне, когда он загружается, но l2 нигде не найти. Я напортачил с LayoutParams? Могу ли я использовать addView для добавления LinearLayout?

11
задан Adam 18 December 2010 в 18:32
поделиться