Android在layout xml中使用include .

在Android的layout樣式定義中,可以使用xml文件方便的實現,有時候爲了模塊的複用,使用include標籤可以達到此目的。
例如:  <include layout="@layout/otherlayout"></div> 

Android開發的官方網站的說明在這裏:https://developer.android.com/resources/articles/layout-tricks-reuse.html
其中,有提到: Similarly, you can override all the layout parameters. This means that any android:layout_* attribute can be used with the <include> tag,意思是任何android:layout_*屬性都可以應用在<include>標籤中。

如果使用如下代碼:
<relativelayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        < textview
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/somestring"
            android:id="@+id/top" />
        
       
        <include < layout="@layout/otherlayout"
        android:layout_below="@id/top" />
        
</relativelayout > 
發現include的otherlayout,並沒有在如我們預期的在id/top這個TextView下面,而是忽略了android:layout_below屬性。經過Google發現,很多人遇到類似的問題。

有解決方法是在include的外面再包一層LinearLayout,如下:
<linearlayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/top" >
        
        < include layout="@layout/otherlayout">
</linearlayout > 
解答道:必須同時重載layout_width和layout_height熟悉,其他的layout_*屬性纔會起作用,否這都會被忽略掉。上面的例子應該寫成這樣:
<include layout="@layout/otherlayout">
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_below="@id/top" />


另外,關於xml的複用,還可以使用merge標籤,merge標籤主要是用來優化顯示的,減少View樹的層級,可以參考這裏:https://developer.android.com/resources/articles/layout-tricks-merge.html, 翻譯版在這裏:http://apps.hi.baidu.com/share/detail/20871363

參考網址:



 
 

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);
這樣就可以了。

作者“earon‘s sky”

 
假使我們遇到這麼一種情況,我們需要開發一個app,這個app的基本所有的Activity都使用到了相同的佈局,我們該如何設計?我們是給這些個Activity佈局文件都統一加上一樣的佈局代碼嗎?但是這樣維護起來很麻煩,修改很不方便,Android佈局中有沒有一種類似於編程語言的include語法呢?答案是有的,但是sdk的demo並沒有說出使用方法,但這並不說明不好使用,其實很簡單。下面的IncludeXmlTest工程給出了樣式。

        我們新建一個IncludeXmlTest工程,我們先從佈局文件上下手,我們新建一個真正的real.xml文件,來實現我們的佈局,代碼如下:

XML/HTML代碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/button"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="show" />  
  12.   
  13.     <TextView  
  14.         android:id="@+id/text"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="" />  
  18.   
  19. </LinearLayout>  

        然後在我們需要引入的xml文件中,include這個real.xml就可以了,我這個main.xml代碼如下:

XML/HTML代碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.    <include   
  8.        android:id="@+id/include"  
  9.        layout="@layout/real"/>  
  10.   
  11. </LinearLayout>  

        這個include部分就是引入了real.xml的佈局,之後我們在程序中只需要佈局main.xml文件即可:

Java代碼
  1. public class IncludeXmlTestActivity extends Activity {  
  2.     private TextView mTextView;  
  3.     private Button mButton;  
  4.       
  5.     /** Called when the activity is first created. */  
  6.     @Override  
  7.     public void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.main);  
  10.           
  11.         initView();  
  12.     }  
  13.       
  14.     /** 
  15.      * 設置view 
  16.      * */  
  17.     public void initView(){  
  18.         mTextView = (TextView) findViewById(R.id.text);  
  19.         mButton = (Button)findViewById(R.id.button);  
  20.         mButton.setOnClickListener(new OnClickListener() {  
  21.               
  22.             @Override  
  23.             public void onClick(View v) {  
  24.                 // TODO Auto-generated method stub  
  25.                 mTextView.setText("hello, i am here");  
  26.             }  
  27.         });  
  28.     }  
  29. }  

        怎麼樣簡單吧,大家在重複佈局的時候一定要記住使用哦,最後看下運行效果:

運行程序

點擊按鈕,顯示textView的文字

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