Android通過在佈局文件中設置include實現xml佈局的複用

效果如圖:


需要的佈局文件title_item:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dip"
    android:background="@android:color/holo_red_light" >

    <ImageView
        android:id="@+id/title_image_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="5dip"
        android:paddingRight="5dip"
        android:paddingTop="5dip"
        android:src="@drawable/back" />

    <TextView
        android:id="@+id/title_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="記事本"
        android:textSize="20sp"
        android:textColor="@android:color/black" />

    <ImageView
        android:id="@+id/title_image_setting"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:paddingBottom="5dip"
        android:paddingLeft="5dip"
        android:paddingTop="5dip"
        android:src="@drawable/more" />

</RelativeLayout>

具體實現的xml代碼:

<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="com.example.mytest.MainActivity" >

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:maxHeight="40dip"
            android:maxWidth="40dip"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/image"
            android:layout_toRightOf="@+id/image"
            android:text="@string/hello_world" />

        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="點擊彈出Menu" />
    </RelativeLayout>

</LinearLayout>



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