While Android offers a variety of reusable lightweight interactive elements such as TextView, you may also want to reuse some of the relatively large, special layout XML. To effectively reuse layout files, you can use include tags and merge tags, which are special layout files that are embedded in the current layout.
Reusable layouts are particularly powerful. For example, a button panel containing the OK and cancel, or a custom progress bar (ProgressBar) accompanying the copy. This means that in an application, a common part of a complex layout can be extracted, managed independently, and then embedded in an include where it is needed. So, you can also create a separate UI component by customizing the view, which is easier to use than the Include method.
Create a re-usable Layout
Create a reusable layout file. (titlebar.xml
)
<framelayoutxmlns:android="Http://schemas.android.com/apk/res/android"
Android:layout_width="Match_parent"
Android:layout_height="Wrap_content"
Android:background="@color/titlebar_bg">
<imageview Android:layout_width="Wrap_content"
Android:layout_height="Wrap_content"
android:src="@drawable/gafricalogo"/>
</FrameLayout>
This root view should be accurate so that the file can be displayed in any of the layout files that have been added to it.
Use the <include> Tag
Here ' s the layout file:
<linearlayoutxmlns:android="Http://schemas.android.com/apk/res/android"
android:orientation="Vertical"
Android:layout_width="Match_parent"
Android:layout_height="Match_parent"
Android:background="@color/app_bg"
android:gravity="Center_horizontal">
<include layout="@layout/titlebar"/>
<textview Android:layout_width="Match_parent"
Android:layout_height="Wrap_content"
Android:text="@string/hello"
android:padding="10DP"/>
...
</LinearLayout>
You can also override the ( android:layout_*
) attribute of any reused view's root view in the include tag. For example:
<include Span style= "COLOR: #882288" >android:id = @+ Id/news_title " android:layout_width " Span style= "COLOR: #666600" >= "match_parent" android:layout_height = " match_parent " layout " Span style= "COLOR: #666600" >= "@layout/title" />
However, once you rewrite, you have to rewrite android:layout_height
andandroid:layout_width
这两个属性,才能让它生效。
The author has a saying: if you need Android Chinese API, please sweep the following QR code, your concern, is my motivation.
Android Chinese Api-include tags