Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.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 以编程方式添加relativelayout_Java_Android_Arrays - Fatal编程技术网

Java 以编程方式添加relativelayout

Java 以编程方式添加relativelayout,java,android,arrays,Java,Android,Arrays,我有以下XML: <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_

我有以下XML:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/rlDateOne"
        android:background="@drawable/dateborder"
        android:padding="@dimen/date_layout_pad" >
        <ImageView
            android:id="@+id/ivIcon"
            android:layout_width="@dimen/cal_icon_size"
            android:layout_height="@dimen/cal_icon_size"
            android:src="@drawable/calicon" />

        <TextView
            android:id="@+id/tvDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/ivIcon"
            android:layout_toRightOf="@+id/ivIcon"
            android:text="@string/date_one"
            android:layout_marginLeft="@dimen/cal_text_left_margin"
            android:textSize="@dimen/cal_text_size"
            android:textColor="@color/dateholiday" />

        <TextView
            android:id="@+id/tvReason"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/ivIcon"
            android:layout_alignLeft="@+id/tvDate"
            android:text="@string/holiday_one"
            android:textSize="@dimen/cal_text_size"
            android:textColor="@color/dateholiday" />

    </RelativeLayout>
    <ImageView
        android:id="@+id/ivCal"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/divider_height"
        android:src="@drawable/divider" />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/rlDateOne"
        android:background="@drawable/dateborder"
        android:padding="@dimen/date_layout_pad" >
        <ImageView
            android:id="@+id/ivIcon"
            android:layout_width="@dimen/cal_icon_size"
            android:layout_height="@dimen/cal_icon_size"
            android:src="@drawable/calicon" />

        <TextView
            android:id="@+id/tvDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/ivIcon"
            android:layout_toRightOf="@+id/ivIcon"
            android:text="@string/date_two"
            android:layout_marginLeft="@dimen/cal_text_left_margin"

            android:textSize="@dimen/cal_text_size"
            android:textColor="@color/dateholiday" />

        <TextView
            android:id="@+id/tvReason"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/ivIcon"
            android:layout_alignLeft="@+id/tvDate"
            android:text="@string/holiday_two"
            android:textSize="@dimen/cal_text_size"
            android:textColor="@color/dateholiday" />

    </RelativeLayout>
    <ImageView
        android:id="@+id/ivCal"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/divider_height"
        android:src="@drawable/divider" />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/rlDateOne"
        android:background="@drawable/dateborder"
        android:padding="@dimen/date_layout_pad" >
        <ImageView
            android:id="@+id/ivIcon"
            android:layout_width="@dimen/cal_icon_size"
            android:layout_height="@dimen/cal_icon_size"
            android:src="@drawable/calicon" />

        <TextView
            android:id="@+id/tvDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/ivIcon"
            android:layout_toRightOf="@+id/ivIcon"
            android:text="@string/date_three"
            android:layout_marginLeft="@dimen/cal_text_left_margin"
            android:textSize="@dimen/cal_text_size"
            android:textColor="@color/dateholiday" />

        <TextView
            android:id="@+id/tvReason"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/ivIcon"
            android:layout_alignLeft="@+id/tvDate"
            android:text="@string/holiday_three"
            android:textSize="@dimen/cal_text_size"
            android:textColor="@color/dateholiday" />

    </RelativeLayout>
   </LinearLayout>

正如您在上面所看到的,同一代码有一个重复(从
ImageView
),这只是一个例子。我必须重复41次,而不是这里显示的3次

  • 如何以编程方式添加XML而不是使用XML(比如说
    tvDate
    tvReason
    是字符串数组)
  • 建议只使用XML还是将其添加到Java中

创建一个列表视图,其中每个项目都有重复的布局。通过这种方式,您可以重复任意多次。

您可以在需要时创建单独的xml文件并放大视图(任意数量):

...
public void onCreate(Bundle b){
super(b);
setContentView(R.layout.your_activity_layout);
...

View yourView = getLayoutInflater().inflate(R.layout.your_xml_file, null);
yourBasicLayout.addView(yourView);
...
}

它不一定必须是ListView。