Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中以编程方式设置堆叠布局_Android_Android Layout - Fatal编程技术网

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

在android中以编程方式设置堆叠布局,android,android-layout,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:orientat

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

这是我的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>
因为我不知道如何嵌套不同的线性布局,所以无论我启动什么都失败了


我可能希望获得有关如何以编程方式进行嵌套的指导,以便能够实现由xml文件生成的相同内容,例如如何实现嵌套。所以,您所需要做的就是将内部项目添加到我的嵌套布局中,并配置边距/填充等。此外,还添加了背景以实现更好的可视化


为什么这个分数偏低
//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);
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);