您的位置:首页 > 移动开发 > Android开发

Android中文API-Include标签

2016-04-25 15:08 465 查看
  


虽然,Android提供了多种多样的可重用轻量级的交互元素(如:TextView 等控件),但是你可能也想重用一些相对较大的特殊布局xml。为了有效的重用布局文件,你可以使用include标签和merge标签,是特殊的布局文件嵌入当前的布局。

可重用的布局是特别强大的。例如一个包含确定和取消的button面板,或者伴随着文案的自定义进度条(progressbar )。这就意味了一款应用中,复杂布局中共同的部分都可以被提取出来,独立管理,然后以include的方式,嵌入需要它的地方。所以,你也可以通过自定义View的方式创建一个独立的UI组件,这样使用起来,比通过include的方式更加简单。

Create a Re-usable Layout

创建一个可重用的布局文件。(
titlebar.xml
)

<FrameLayout
xmlns: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>

为了让这个文件可以在任何一个添加了它的布局文件中展示,这个root View应该是准确的。
Use the <include> Tag

Here's the layout file:

<LinearLayout
xmlns: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>

你也可在include标签中重写任何被重用的view的root view的(
android:layout_*
 )属性。例如:

<include android:id="@+id/news_title"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         layout="@layout/title"/>


然而,你一旦重写,你必须要重写
android:layout_height
 and 
android:layout_width
这两个属性,才能让它生效。

 作者有话说:如果您需要Android中文API,请扫一扫下面的二维码,您的关注,就是我的动力。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: