簡體   English   中英

以編程方式添加relativelayout

[英]Add relativelayout programmatically

我有以下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文件並膨脹視圖(任何數量):

...
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,其中每個項目都具有重復的布局。 通過這種方式,您可以根據需要重復多次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM