Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.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
Java 如何以编程方式添加下面的布局_Java_Android_Android Layout_Android Studio - Fatal编程技术网

Java 如何以编程方式添加下面的布局

Java 如何以编程方式添加下面的布局,java,android,android-layout,android-studio,Java,Android,Android Layout,Android Studio,我有下面的xml布局。但我想用相同的布局动态地添加几个类似的行(相对地,一行一行地放在另一行下面)。我尝试了下面的代码,但是所有的值都是重叠的 此外,我不知道如何以编程方式将textview一个添加到另一个下面。 解决这个问题的任何帮助都会很有帮助。提前谢谢 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

我有下面的xml布局。但我想用相同的布局动态地添加几个类似的行(相对地,一行一行地放在另一行下面)。我尝试了下面的代码,但是所有的值都是重叠的

此外,我不知道如何以编程方式将textview一个添加到另一个下面。 解决这个问题的任何帮助都会很有帮助。提前谢谢

<?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);
对于(int i=0;我只是用以下代码替换您的代码:
下面是java代码

    setContentView(R.layout.myview_activity);

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

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

        LinearLayout row= new LinearLayout(this);

        LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        layoutParams.setMargins(20, 5, 20, 5);
        row.setLayoutParams(layoutParams);
        row.setBackgroundColor(Color.parseColor("#000000"));

        LinearLayout.LayoutParams layoutParams1=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams1.setMargins(0,0,20,0);

        TextView name = new TextView(this);
        name.setLayoutParams(layoutParams1);

        TextView time = new TextView(this);
        time.setLayoutParams(layoutParams1);

        TextView dest = new TextView(this);
        dest.setLayoutParams(layoutParams1);


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

        row.addView(name);
        row.addView(time);
        row.addView(dest);

        ll.addView(row);
    }

}
setContentView(R.layout.myview\u活动);
LinearLayout ll=(LinearLayout)findViewById(R.id.ll);
对于(int i=0;我只是用以下代码替换您的代码:
下面是java代码

    setContentView(R.layout.myview_activity);

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

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

        LinearLayout row= new LinearLayout(this);

        LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        layoutParams.setMargins(20, 5, 20, 5);
        row.setLayoutParams(layoutParams);
        row.setBackgroundColor(Color.parseColor("#000000"));

        LinearLayout.LayoutParams layoutParams1=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams1.setMargins(0,0,20,0);

        TextView name = new TextView(this);
        name.setLayoutParams(layoutParams1);

        TextView time = new TextView(this);
        time.setLayoutParams(layoutParams1);

        TextView dest = new TextView(this);
        dest.setLayoutParams(layoutParams1);


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

        row.addView(name);
        row.addView(time);
        row.addView(dest);

        ll.addView(row);
    }

}
setContentView(R.layout.myview\u活动);
LinearLayout ll=(LinearLayout)findViewById(R.id.ll);
对于(int i=0;i
LinearLayout ll=(LinearLayout)findViewById(R.id.searchResultsLayout);
ll.设置方向(线性布局、垂直);
对于(int i=0;i
LinearLayout ll=(LinearLayout)findViewById(R.id.searchResultsLayout);
ll.设置方向(线性布局、垂直);

对于(iTi=0;i为了获得相对布局,另一个只使用ListVIEW和Advor。当你获得数据

时,它会自动生成,以获得相对的布局,一个只使用ListVIEW和适配器。如果你想要一个你应该考虑的物品列表,它会自动生成。使用一个合适的适配器为您处理事情,创建一个

RecyclerView
ListView
。在您的代码中,for循环没有任何意义,它是重叠的,因为您没有像为定义其位置的
RelativeLayout
行设置
布局参数那样以编程方式设置
布局参数刚才使用的是如何在程序的另一个下面添加RelayLayOut1?如果你想有一个项目列表,你可能需要考虑使用<代码> RealReVIEW 或 ListVIEW < /Cord>,用一个合适的适配器来处理你的事情。代码中没有循环的含义,它是重叠的,因为你没有设置<代码>文本视图。
LayoutParams
编程方式与您对
RelativeLayout
行所做的一样,它定义了它们的位置我刚才使用了如何通过编程方式将RelativeLayout一个一个添加到另一个下面?嗨,Nikhil,代码生成了与问题中上述XML类似的视图嗨,Nikhil,代码生成了视图sim问题中的上述XML的ilar
 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);
    }