How to programmatically add multiple LinearLayouts into one view and then add to ViewFlipper?

只谈情不闲聊 提交于 2019-12-04 10:46:22

问题


I hope question title gives you a good description of the problem.

I want to create this XML, but programatically (please don't suggest not doing it programmatically ^_^ )

<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>

As you can see, there are two LinearLayout's, one embedded in the other. I need to reproduce this and then add it to a ViewFlipper.

This is what I have so far:

     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);

Where flipper is defined as:

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

Without a problem, I can see "l1" in the window when it loads up, but l2 is no where to be found. Did I mess up with LayoutParams? Can I use addView to add a LinearLayout?


回答1:


I think you forgot to set layout weight (only my opinion though, could be wrong). because you are adding more views with layout-height set to FILL_PARENT



来源:https://stackoverflow.com/questions/4479429/how-to-programmatically-add-multiple-linearlayouts-into-one-view-and-then-add-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!