Android佈局優化之include

在我們佈局的過程中經常會碰到很多畫面非常類似,僅僅是文字什麼的不同,象一些導航啊,菜單啊之類的。如果有過html經驗的同學都知道有include可以導入公告部分,這樣能大大的減少代碼的重複,提高複用率。那麼在Android中是否也能這樣呢? 答案是肯定的。

現在我們直接看代碼。

  • 首先看看目錄結構
    這裏寫圖片描述

  • 在目錄結構裏我們可以看到除了main_activity.java文件和activity_main.xml佈局文件之外,還有一個commen.xml文件。這個文件就是一個公共導航,我們來看一下佈局:
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:background="#000000"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:layout_height="wrap_content">
    
        <TextView
            android:id="@+id/back"
            android:text="返回"
            android:textColor="#ffffff"
            android:textSize="15sp"
            android:layout_marginLeft="10dp"
            android:layout_alignParentLeft="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <TextView
            android:id="@+id/title"
            android:text="標題"
            android:textColor="#ffffff"
            android:textSize="17sp"
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <TextView
            android:id="@+id/fun"
            android:text="功能"
            android:textColor="#ffffff"
            android:textSize="15sp"
            android:layout_marginRight="10dp"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>


  • 佈局文件寫完之後我們看看activity_main.xml這個佈局文件中怎麼引入這個commen.xml的:

    <span style="color:#3f3f3f;"><?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">
    
       </span><span style="color:#ff0000;"> <include layout="@layout/commen"></include></span><span style="color:#3f3f3f;">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!" />
    </LinearLayout>
    </span>

    注意標紅的那一行代碼,直接include標籤 加一個layout屬性,就把commen這個公共文件導入了。同時我們在這個導入的下面還加了個TextView,現在我們來看看運行的效果:



  • 就這樣就實現了include的導入,那麼有一個問題,我們在activity_main.xml這個佈局文件中導入了commen.xml這個公共的頭文件,那在MainActivity中是否能直接用commen裏面的控件呢? 這裏我們做了個嘗試:

    package com.example.lolli.test;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity{
        // 定義include進來的textView
        private TextView backText;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // 初始化TextView
            backText = (TextView) findViewById(R.id.back);
    
            //  設置TextView的點擊事件
            backText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "你點擊了返回按鈕", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
    

    我對頭部的返回的TextView做了初始化工作,並設置了點擊事件,我們看看運行的效果:


    點擊按鈕生效了。其實include是把整個頁面都弄進當前佈局文件了,就是當前佈局文件的一分子,我們能直接使用。

發佈了53 篇原創文章 · 獲贊 4 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章