1. 程式人生 > >Android中xml佈局檔案中使用include引入佈局進行復用(解決使用include佈局重疊,顏色設定無效問題)

Android中xml佈局檔案中使用include引入佈局進行復用(解決使用include佈局重疊,顏色設定無效問題)

使用include引入佈局的作用

  提取重複的佈局程式碼,方便進行復用

如何使用

  比如我們想要線上性佈局中建立三塊需要複用的佈局headview、centerview、buttomview(用Button簡單代替)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height
="match_parent" android:orientation="vertical" tools:context=".MainActivity">
<Button android:id="@+id/headview" android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/centerview" android:layout_width
="match_parent" android:layout_height="wrap_content"/>
<Button android:id="@+id/buttomview" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>

  程式碼寫出來了,但其他佈局如果也要使用centerview,就需要將佈局拷貝過去,導致程式碼冗餘,但使用include引入centerview就可以讓其他佈局進行復用

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <include layout="@layout/headview"
             android:id="@+id/headview"/>

    <include layout="@layout/centerview"
             android:id="@+id/centerview"/>

    <include layout="@layout/buttomview"
             android:id="@+id/buttomview"/>

</LinearLayout>

  於是,我們就採用如下方法引入headview、centerview、buttomview,但是我們會發現,這三個佈局重疊到一起去了,並沒有像我們想象的上下堆疊排列

include使用注意事項

  1,必須同時過載layout_width和layout_height屬性,其他的屬性才會起作用,否則會被忽略掉。
  2,include裡面設定背景顏色是不生效的,必須在include的佈局裡面寫,include裡面設定位置及大小資訊,然後直接在include的佈局裡面設定match_parent即可

  於是include引入佈局程式碼正確寫法如下:

//(layout)activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <include layout="@layout/headview"
             android:id="@+id/headview"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"/>

    <include layout="@layout/centerview"
             android:id="@+id/centerview"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"/>

    <include layout="@layout/buttomview"
             android:id="@+id/buttomview"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"/>
</LinearLayout>
-------------------------------------------------------------------
//(layout)headview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <Button
        android:id="@+id/headview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>
-------------------------------------------------------------------
//(layout)centerview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <Button
        android:id="@+id/centerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>
-------------------------------------------------------------------
//(layout)buttomview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <Button
        android:id="@+id/buttomview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

———-因本人才疏學淺,如部落格或Demo中有錯誤的地方請大家隨意指出,與大家一起討論,共同進步,謝謝!———-