我想制作一个模块化的 LinearLayout Xml 文件,以便我可以在多个片段中使用它。
要做到这一点,我应该怎么做?
你必须使用include
标签。让我们在layout
目录中创建 2 个文件
第一的
res > layout > first_element.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<View
android:layout_width="50dp"
android:layout_height="30dp"
android:layout_margin="20dp"
android:background="@color/colorPrimary" />
<View
android:layout_width="50dp"
android:layout_height="30dp"
android:layout_margin="20dp"
android:background="@color/colorPrimary" />
<View
android:layout_width="50dp"
android:layout_height="30dp"
android:layout_margin="20dp"
android:background="@color/colorPrimary" />
</LinearLayout>
第二
res > layout > second_element.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:textSize="30sp"
android:text="FIRST"
android:textColor="@color/colorAccent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:textSize="30sp"
android:text="SECOND"
android:textColor="@color/colorAccent" />
</LinearLayout>
之后,您可以使用include
标签在第三个布局文件中重用它们:
第三
res > layout > main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/first_element"/>
<include layout="@layout/second_element"/>
</LinearLayout>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句