android布局採用include 統一管理布局

來源:互聯網
上載者:User

標籤:

通常的在一個項目中會有頂部標題列 和 底部導覽列。而且這些東西在很多個介面都有使用。

我們採用include 來進行布局的重用。

include標籤常用於將布局中的公用部分提取出來供其他layout共用,以實現布局模組化。

<include />標籤可以使用單獨的layout屬性,前提是必須給其設定 android:layout_width 和 android:layout_height 兩個屬性。否則沒效果。


Activity需要的布局檔案:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="wrap_content" >    <!--        我的QQ :2622596982    郵箱:[email protected]  QQ 交流群 : 277599214tips :歡飲各位朋友多多提意見,小弟不勝感激    -->    <!-- 這裡是頂部標題列的布局(只需要修改 include_top_title 布局中的內容就可以了,在這裡不需要做任何的變動) -->    <include        android:id="@+id/il_top"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        layout="@layout/include_top_title" />    <!-- 這裡是底部導覽列欄的布局(只需要修改 include_bottom_navigation 布局中的內容就可以了,在這裡不需要做任何的變動) -->    <include        android:id="@+id/il_bottom"        android:layout_width="match_parent"        android:layout_height="60dp"        android:layout_alignParentBottom="true"        layout="@layout/include_bottom_navigation" />    <!-- 中間部分要展示的內容,只需要把要在該地區展示的介面布局include進來就可以了,例如:layout="@layout/include_bottom_navigation" (不管是自訂的還是系統內建的,只需要這麼一句話就可以了) -->    <include        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_above="@id/il_bottom"        android:layout_below="@id/il_top"        layout="@layout/include_bottom_navigation" /></RelativeLayout>



定義一個頂部標題列的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="@android:color/darker_gray"    android:paddingBottom="8dp"    android:paddingTop="8dp" >    <ImageView        android:id="@+id/iv_back"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_marginLeft="8dp"        android:background="@drawable/back" />    <ImageView        android:id="@+id/iv_go"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_marginRight="8dp"        android:background="@drawable/go"/>    <TextView        android:id="@+id/tv_title"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        android:text="這裡是標題" /></RelativeLayout>

然後我們在 activity 的布局檔案中把該頂部標題列的布局include進來就行了:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="wrap_content" >    <include        layout="@layout/include_top_title" /></RelativeLayout>

如下:



最後順便將底部導覽列  和 中間內容顯示地區也封裝了吧。

底部導覽列:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="@android:color/background_dark"    android:orientation="horizontal" >    <ImageView        android:layout_height="wrap_content"        android:layout_weight="1"        android:background="@drawable/home" />    <ImageView        android:layout_height="wrap_content"        android:layout_weight="1"        android:background="@drawable/home" />    <ImageView        android:layout_height="wrap_content"        android:layout_weight="1"        android:background="@drawable/home" />    <ImageView        android:layout_height="wrap_content"        android:layout_weight="1"        android:background="@drawable/home" /></LinearLayout>


最終的如下:



最後附上:

點我下載








android布局採用include 統一管理布局

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.