1. 程式人生 > >Android一個佈局檔案巢狀另一個佈局檔案的方法

Android一個佈局檔案巢狀另一個佈局檔案的方法

文前:學安卓與JAVA算來已經有兩個月了,以前都是看著書本或者跟著老師打程式碼,真正自己去寫程式碼卻很少,今天自己嘗試著寫一個小專案時卻發現這樣和那樣的問題,甚至是一些小問題都要花掉兩三個小時去查資料解決今天被一個佈局問題搞得焦頭爛額,覺得是時候該做下筆記,讓自己長長記性了。

在寫佈局檔案時,經常會有一部分程式碼是重複的,為了提高程式碼的複用性和減少程式碼量,我們通常的做法是將相同的那一部分寫入一個layout檔案中讓其它佈局檔案去呼叫,這時我們在呼叫的佈局檔案用到<include />佈局,在該佈局里加入layout="@layout/“被引用的佈局檔名”"以我做的Demo為例子。

這是我想要做成的效果,而我中間的一些圖片控制元件用到了絕對佈局相對比較複雜於是我將那六邊形的圖片控制元件單獨拿出來寫成了一個佈局menu_icon.xml。

程式碼如下:

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


    <ImageView
        android:id="@+id/music_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="115dip"
        android:layout_y="0dip"
        android:src="@drawable/music_icon_1_0" />
    <ImageView
        android:id="@+id/phone_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="189dip"
        android:layout_y="36dip"
        android:src="@drawable/phone_icon_2_0" />
    <ImageView
        android:id="@+id/clock_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="189dip"
        android:layout_y="117dip"
        android:src="@drawable/clock_icon_3_0" />
    <ImageView
        android:id="@+id/battery_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="115dip"
        android:layout_y="152dip"
        android:src="@drawable/batery_icon_4_0" />
    <ImageView
        android:id="@+id/camera_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="40dip"
        android:layout_y="117dip"
        android:src="@drawable/camera_icon_5_0" />
    <ImageView
        android:id="@+id/email_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="40dip"
        android:layout_y="36dip"
        android:src="@drawable/email_icon_0_0" />


</AbsoluteLayout>

讓主介面佈局main_menu.xml去引用它,程式碼如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background" >


    <!-- 位於頂部的標題圖片 -->


    <ImageView
        android:id="@+id/title_map"
        android:layout_width="200dip"
        android:layout_height="200dip"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dip"
        android:src="@drawable/title" />


    <!-- 定義一個Textview在父控制元件的中間 -->


    <TextView
        android:id="@+id/center_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="記事本"
        android:layout_marginLeft="150dip"
        android:layout_marginTop="300dip"
        android:textColor="#0e1ff4"
        android:textSize="20sp" />

    <include
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="200dip"
        layout="@layout/menu_icon" />


</RelativeLayout>

相關推薦

no