java - 以编程方式添加相对布局

标签 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_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</RelativeLayout>,这只是一个示例。我必须重复 41 次,而不是此处显示的 3 次。

  • 我如何以编程方式添加它而不是使用 XML(假设 tvDatetvReason 是字符串数组)?
  • 建议只使用 XML 还是在 Java 中添加它?

最佳答案

您可以在需要时创建单独的 xml 文件并扩展 View (任意数量):

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

关于java - 以编程方式添加相对布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19083863/

相关文章:

java - 字符串的替代表示

java - 可序列化的类无法反序列化

android - Android 上的 Cordova FCM

ios - 在将数组发送到 API 端点之前序列化数组

c - Malloc 影响随机整数值

java - 如何使用Hibernate JAVA调用mysql存储过程

java - 如何动态定义可用作方法中参数的类?

来自非 Activity 类的非 Activity 类中的Android getResource?

Android:对于 Canvas 动画,最好有 1 个大线程,或多个小线程

c - 将文件中的数字读入数组