[英]Android include layout below element
我想在圖像中創建一個布局。 它不包括他,所以代碼很長。 我想為每個BOX創建一個布局,並將其包含在主布局中,一個在另一個下面。
問題是XML文件這種類型的布局很長。 所以我會使用包含布局並創建一個新的布局,然后重復包含,例如:
我有一個RelativeLayout,我有一個用於line1的ImageView,下面我想在這個方法中設計框以減少代碼:
<include layout = "box1"
layoutBelow = "linea1"
/>
方框2也是如此:
<include layout = "box2"
layoutBelow = "linea2"
/>
但我包含的布局並沒有像我想的那樣對齊。 布局疊加在現有布局上。
我用這種方式解決了:
<!-- LINE SEPARATOR 1-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/logo"
android:id="@+id/linea1"
android:background="@drawable/linea"
/>
<!-- BOX1 -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/linea1"
>
<include
layout="@layout/box1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
</RelativeLayout>
<!-- LINE SEPARATOR 2-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/logo"
android:id="@+id/linea2"
android:background="@drawable/linea"
/>
<!-- BOX2 -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/linea2"
>
<include
layout="@layout/box2"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
</RelativeLayout>
結果是這個圖像:
謝謝大家:)
Android Studio中告訴我,這些類型包括工作是layout_width
和layout_height
也必須在包括標簽中指定,否則layout_below
被忽略。
所有最后的答案都有一些糾正,首先如果你想使用id
, linea1
布局必須有它。 例如:
<LinearLayout
android:id="@+id/id_linea1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
...
</LinearLayout>
並包括它使用
<include
layout="@layout/box1"
android:layout_below="@id/id_linea1"
/>
linea2也必須低於box1,才能在RelativeLayout上正常工作。
也許具有垂直方向的LinearLayout會更好地滿足您的興趣。
您可以像任何視圖一樣執行此操作:
<include
layout="@layout/content_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/id_linea1" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.