如何在ScrollView中以编程方式添加LinearLayout和RelativeLayout?

时间:2014-08-24 11:16:04

标签: android android-linearlayout relativelayout

如何以编程方式在LinearLayout中添加RelativeLayoutScrollView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/scrollID"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >


    </ScrollView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="true"
        android:orientation="horizontal"
        android:paddingBottom="5dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:weightSum="1.5" 
        android:background="@drawable/bottom_conversation">

           <TextView
            android:id="@+id/isTyping"
            android:layout_width="0dip"
            android:layout_weight="0.5"
            android:layout_height="wrap_content"
            android:hint="Chat" />

        <EditText
            android:id="@+id/txtInpuConversation"
            android:layout_width="200dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:hint="@string/edt_Conversation" >

            <requestFocus />
        </EditText>

        <ImageButton
            android:id="@+id/someID"
            android:layout_width="0dip"
            android:layout_weight="0.5"
            android:layout_height="wrap_content"
            android:background="@drawable/background_selector"
            android:contentDescription="@null"
            android:layout_marginTop="5dp"
            android:src="@drawable/social_send_now" />
    </LinearLayout>

</LinearLayout>

注意:我需要在RelativeLayout

中插入LinearLayout

2 个答案:

答案 0 :(得分:2)

TextView tv = new TextView(this);
tv.setText("Testsssssssssssssss");
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setId(1);

RelativeLayout relativeLayout = new RelativeLayout(this);
LayoutParams LLParamsT = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(LLParamsT);
relativeLayout.addView(tv,
        new LayoutParams(LayoutParams.WRAP_CONTENT,
               LayoutParams.WRAP_CONTENT));

LinearLayout firstLinearLayout= new LinearLayout(this);
firstLinearLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams LLParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
firstLinearLayout.setLayoutParams(LLParams);
firstLinearLayout.addView(relativeLayout);


ScrollView sclView = (ScrollView) findViewById(R.id.scrollID);
sclView.addView(firstLinearLayout, new
          LayoutParams(LayoutParams.WRAP_CONTENT,
                     LayoutParams.WRAP_CONTENT));

祝你好运......

答案 1 :(得分:-2)

您可以使用

添加视图(LinearLayout,RelativeLayout ...)
scrollView.addView(newLinearLayoutView);