这个用法还是相当有用的,除了可以分工明确之外,也可以防止布局文件过大。
actvity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include
android:id="@+id/my_img_layout"
layout="@layout/img_layout"
/>
<include
android:id="@+id/my_text_layout"
layout="@layout/text_layout"
/>
</LinearLayout>
img_layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/news_small_default"
/>
</LinearLayout>
text_layout:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hello,you"
/>
</merge>
效果: