如何以编程方式添加多个LinearLayouts到一个视图,然后添加到ViewFlipper? [英] How to programmatically add multiple LinearLayouts into one view and then add to ViewFlipper?

查看:117
本文介绍了如何以编程方式添加多个LinearLayouts到一个视图,然后添加到ViewFlipper?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望问题标题为您提供了该问题的一个很好的说明。

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

我想创建这个XML,但编程(请不要建议不要做编程^ _ ^)

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>

正如你所看到的,有两个的LinearLayout的,一是嵌入到其他。我要重现此,然后将其添加到ViewFlipper。

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.

这是我到目前为止有:

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

如果没有问题,我可以看到L1窗口时,它加载了,但12是没有地方可以找到。难道我乱用的LayoutParams?我可以使用addView添加的LinearLayout?

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?

推荐答案

我想你忘记了设置布局重量(只是我的意见,虽然,可能是错误的)。 因为你将与布局的高度设置为FILL_PARENT更多的意见

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

这篇关于如何以编程方式添加多个LinearLayouts到一个视图,然后添加到ViewFlipper?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆