Android手機應用開發之include標籤使用

來源:互聯網
上載者:User

標籤:android   手機開發   include標籤   

android中include標籤是為了便於控制項的覆用的一個很好解決方案。 
但是也有一些需要注意的地方,下面是本人在項目中碰到過的一個問題,做此記錄,便於以後查看。 
include標籤用法。 
1.建立一個xml檔案,命名 head.xml 
head.xml檔案內容如下: 
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
                android:id="@+id/index_linear_foot" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 

        <ImageView 
            android:id="@+id/head_btn_refresh" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
        /> 
</RelativeLayout> 
2.建立一個布局檔案,命名 main.xml 
main.xml檔案內容如下: 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 

<include android:layout_width="fill_parent" android:layout_height="wrap_content" layout="@layout/head" /> 
</LinearLayout> 
注意:上面我們的include標籤中是沒有為它指定id的。 
3.建立一個MainActivity,設定布局檔案為main.xml; 
4.假設我現在需要在代碼中為head.xml中的RelativeLayout容器設定背景圖片。 
代碼如下: 
//獲得版面配置容器對象 
RelativeLayout head = (RelativeLayout)findViewById(R.id.index_linear_foot); 
//設定背景圖片 
head.setBackgroundResource(R.drawable.head); 
這樣就OK了。 
5.剛剛說到,我們的include標籤中是沒有為它指定id的,假設我們現在的main.xml檔案版面配置容器是RelativeLayout,而我需要把某個控制項放在head.xml下面。就需要使用到RelativeLayout版面配置容器的特有屬性 android:layout_below="" 屬性。還需要為include指定id屬性。 
那我們的main.xml檔案變成如下: 
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<include android:id="@+id/main_head" android:layout_width="fill_parent" android:layout_height="wrap_content" layout="@layout/head" /> 
<ListView 
android:id="@+id/listview" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_below="@+id/main_headb" 
/> 
</RelativeLayout> 
那接下來我們在運行我們的執行個體,結果發現,代碼在運行到head.setBackgroundResource(R.drawable.head); 
這一句的時候拋異常了 
java.lang.NullPointerException 
原來:如果include指定了id的話,就不能直接把它裡面的控制項當成主xml中的控制項來直接獲得了,必須先獲得這個xml布局檔案,再通過布局檔案findViewById來獲得其子控制項。 
代碼如下 
View layout = getLayoutInflater().inflate(R.layout.head, null); 
RelativeLayout head= (RelativeLayout)layout.findViewById(R.id.index_linear_foot); 
//設定背景圖片 
head.setBackgroundResource(R.drawable.head); 
這樣就可以了。

Android手機應用開發之include標籤使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.