在android中以编程方式设置堆叠布局

时间:2016-09-23 16:22:11

标签: android android-layout

我想有一个相对布局,线性布局嵌套在它们之间,但我无法理解如何以编程方式嵌套它们

这就是我的xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
    android:id="@+id/button_top"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_margin="5dp"
    android:text="Add items" />

<LinearLayout
    android:id="@+id/above_part"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button_top"
    android:gravity="center_horizontal"
    android:orientation="horizontal"
    android:weightSum="1">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:orientation="vertical"
        android:padding="2dp">

        <TextView
            android:id="@+id/label_farmerprovince"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Province"
            android:textSize="12sp"
            android:textStyle="normal" />

        <EditText
            android:id="@+id/fivms_farmerprovince"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:drawable/editbox_background">

        </EditText>

    </LinearLayout>


    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:orientation="vertical"
        android:padding="2dp">

        <TextView
            android:id="@+id/distric_label"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="District"
            android:textSize="12sp"
            android:textStyle="normal" />

        <EditText
            android:id="@+id/district"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:drawable/editbox_background" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:orientation="vertical"
        android:padding="2dp">

        <TextView
            android:id="@+id/label_farmeragriblocks"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Agriblocks"
            android:textSize="12sp"
            android:textStyle="normal" />

        <EditText
            android:id="@+id/agri"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:drawable/editbox_background" />

    </LinearLayout>

</LinearLayout>

这会产生我想要的布局: enter image description here

现在我想添加上面的布局并将其更改为java方式

我试过了

//top layout
        RelativeLayout newlayout = new RelativeLayout(getContext());
        LinearLayout belowlayout = new LinearLayout(getContext());
        LinearLayout above_part = new LinearLayout(getContext()); //lin with id of above_part
        LinearLayout in_above_part_one = new LinearLayout(getContext()); //lin with id of above_part

        //layouts properties
        belowlayout.setOrientation(LinearLayout.HORIZONTAL);

        //setids for the layouts
        newlayout.setId(12);
        belowlayout.setId(13);

        //Button
        Button additemsbtn = new Button(getContext());
        additemsbtn.setText("Log In");
        additemsbtn.setBackgroundColor(Color.BLACK);

        //Username input
        EditText username = new EditText(getContext());
        additemsbtn.setId(int 1);
        username.setId(2);

        RelativeLayout.LayoutParams buttonDetails = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT,

        );

        RelativeLayout.LayoutParams usernameDetails = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT
        );

        //Give rules to position widgets
        usernameDetails.addRule(RelativeLayout.ABOVE, additemsbtn.getId());
        usernameDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
        usernameDetails.setMargins(0,0,0,50);

        buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
        buttonDetails.addRule(RelativeLayout.CENTER_VERTICAL);

由于无法知道如何嵌套不同的线性布局,无论我开始失败了什么,

我可能会喜欢有关如何以编程方式执行此嵌套的指南,以便我可以实现xml文件生成的相同内容

1 个答案:

答案 0 :(得分:1)

如何做到这一点的小例子。因此,您需要做的就是将内部项添加到我的嵌套布局并配置边距/填充等。还添加了背景以实现更好的可视化enter image description here

RelativeLayout root = (RelativeLayout) findViewById(R.id.root);

    Button addButton = new Button(this);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    addButton.setLayoutParams(params);
    addButton.setId(123);
    root.addView(addButton);


    LinearLayout rootLinearlayout = new LinearLayout(this);
    RelativeLayout.LayoutParams linearRootParams = new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    linearRootParams.addRule(RelativeLayout.BELOW, 123);
    rootLinearlayout.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent));
    rootLinearlayout.setLayoutParams(linearRootParams);

    // Nested Linears
    LinearLayout linearOne = new LinearLayout(this);
    LinearLayout linearTwo = new LinearLayout(this);
    LinearLayout linearThree = new LinearLayout(this);
    LinearLayout.LayoutParams linearParamsWithWeight = new LinearLayout.LayoutParams(
            0,
            300);
    linearParamsWithWeight.weight = 1;

    linearOne.setBackgroundColor(ContextCompat.getColor(this, android.R.color.black));
    linearTwo.setBackgroundColor(ContextCompat.getColor(this, android.R.color.holo_red_dark));
    linearThree.setBackgroundColor(ContextCompat.getColor(this, android.R.color.holo_blue_dark));

    linearOne.setLayoutParams(linearParamsWithWeight);
    linearTwo.setLayoutParams(linearParamsWithWeight);
    linearThree.setLayoutParams(linearParamsWithWeight);

    rootLinearlayout.addView(linearOne);
    rootLinearlayout.addView(linearTwo);
    rootLinearlayout.addView(linearThree);