如何以编程方式添加以下布局 [英] How to add below layout programatically

查看:67
本文介绍了如何以编程方式添加以下布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在xml中有以下布局.但是我想用相同的布局动态添加更多类似的行(RelativeLayouts在另一行下面).我尝试了下面的代码,但是所有值都是重叠的.

我也不知道如何以编程方式在另一个视图下面添加textview. 解决此问题的任何帮助将是有帮助的.预先感谢.

I have below layout in xml. But I want to add few more similar rows(RelativeLayouts one below another) dynamically with same layout. I've tried the below code, but all the values are overlapping.

Also I don't know how to add textview one below another programmatically. Any help in solving this would be helpful. Thanks in advance.

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

    <LinearLayout
        android:id="@+id/searchResultsLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:background="#fff"
            android:layout_marginStart="20dp"
            android:layout_marginEnd="20dp"
            android:layout_height="100dp"
            android:padding="6dip" >

            <TextView
                android:id="@+id/firstLine"
                android:layout_width="fill_parent"
                android:layout_height="26dp"
                android:ellipsize="marquee"
                android:text="Ali Connors"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/secondLine"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/firstLine"
                android:gravity="center_vertical"
                android:text="Brunch this weekend?"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/rightTime"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:text="15m"
                android:textSize="16sp" />

        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

代码:

LinearLayout ll = (LinearLayout) findViewById(R.id.searchResultsLayout);

        for (int i = 0; i <1; i++) {

            RelativeLayout row= new RelativeLayout(this);
            RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
            relativeParams.setMargins(20, 5, 20, 5);

            row.setBackgroundColor(Color.parseColor("#ffffff"));
            ll.setLayoutParams(relativeParams);
            TextView name = new TextView(this);
            TextView time = new TextView(this);
            TextView dest = new TextView(this);

            name.setText("Name");
            time.setText("9hrs");
            dest.setText("Destination");
            row.addView(name);
            row.addView(time);
            row.addView(dest);
            ll.addView(row,i);
        }

推荐答案

 LinearLayout ll = (LinearLayout) findViewById(R.id.searchResultsLayout);
    ll.setOrientation(LinearLayout.VERTICAL);

    for (int i = 0; i <1; i++) {

        RelativeLayout row= new RelativeLayout(this);
        RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        relativeParams.setMargins(20, 5, 20, 5);

        row.setBackgroundColor(Color.parseColor("#ffffff"));
        ll.setLayoutParams(relativeParams);
        TextView name = new TextView(this);
        TextView time = new TextView(this);
        TextView dest = new TextView(this);
        final RelativeLayout.LayoutParams paramsval =
                new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams
                (RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);

        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        name.setText("Name");
        name.setId(View.generateViewId());

        time.setText("9hrs");
        dest.setText("Destination");

        time.setLayoutParams(layoutParams);
        row.addView(time);
        row.addView(name);
        paramsval.addRule(RelativeLayout.BELOW,name.getId());
        dest.setLayoutParams(paramsval);
        row.addView(dest);
        ll.addView(row,i);
    }

这篇关于如何以编程方式添加以下布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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