如何以编程方式将多个LinearLayouts添加到一个视图中,然后添加到ViewFlipper?

时间:2010-12-18 18:32:28

标签: android layout android-linearlayout viewflipper

我希望问题标题可以很好地描述问题。

我想以编程方式创建此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);

将鳍状肢定义为:

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

没有问题,我可以在窗口加载时看到“l1”,但是l2无处可寻。我搞乱了LayoutParams吗?我可以使用addView添加LinearLayout吗?

1 个答案:

答案 0 :(得分:10)

我认为你忘了设置布局重量(虽然我的看法可能是错的)。 因为您要添加更多布局高度设置为FILL_PARENT

的视图