xml中使用include引入佈局

爲了複用佈局,使用include方式引用

activity_top_bar.xml 代碼如下
需要注意的是:父容器LinearLayout中layout_height爲wrap_content,而不是match_parent,以免引入到其他xml,會將整個界面覆蓋掉

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#1f75fe"
        >
        <ImageView
            android:id="@+id/iv_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon_back"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="20dp"
            />
        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_gravity="center_vertical|center_horizontal"

            />


    </FrameLayout>



</LinearLayout>

寫完後如下面這樣,textview沒寫什麼東西,可以忽略,如果需要在各種佈局顯示當前頁面名稱,就可以調用使用
在這裏插入圖片描述
activity_personal_information 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"

    >


    <include layout="@layout/activity_top_bar" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="這是測試。。。。。"
        />



</LinearLayout>

效果如下:在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章