在Android中以编程方式设置堆叠布局 [英] Setting stacked up layout programmatically in android

查看:79
本文介绍了在Android中以编程方式设置堆叠布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to have a relative layout with linear layouts nested in between them but i am unable to understand how ill nest them programmatically

这是我作为xml文件所拥有的:

This is what i have as my xml file:

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

这将产生我想要的布局:

This produces the layout that i want:

现在我想添加以上布局并将其更改为Java方式

Now i would like to add the above layout and change it to a java way

我尝试过

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

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

Whatever i have started fails since am unable to know how to nest the different linear layouts,

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

I would probably like guidelines on how to do this nesting programmatically so that i can achieve the same thing generated by the xml file

推荐答案

如何完成此示例的小例子.因此,您要做的就是将内部项目添加到我的嵌套布局中并配置边距/填充等.还添加了背景以实现更好的可视化.

Little example how it can be done. So, all you need to do is to add inner items to my nested layouts and configure margins/paddings etc. Also added background for better visualization.

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

这篇关于在Android中以编程方式设置堆叠布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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