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

11

希望问题标题已经清楚地描述了问题。

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


使用层次结构查看器来帮助调试您动态生成的布局:http://developer.android.com/guide/developing/tools/hierarchy-viewer.html - CommonsWare
@CommonsWare:谢谢。我从未知道它的存在,更不用说它可以解决问题了! - Adam
看起来你正在将View(s)添加到l1中。 我建议尝试直接使用addView(View v)将它们添加到Flipper中。 - user636638
1个回答

10

我认为您忘记了设置布局权重(仅代表个人观点,可能是错误的)。因为您正在添加更多布局高度设置为FILL_PARENT的视图。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接