Android实战经验之Incude便签

当我们做项目时经常会用到相同的布局设计,如果都写在一个xml文件中,代码显得很冗余,,让人有一种去死的感觉,可读性也很差。

所以我们可以把相同布局的代码单独拿出来放在一个xml文件中,通过<include /> 标签来重用它。这样我们的代码显得比较清洁,一目了然。

读者对代码的整体布局有一个深入的了解。

1 include标签只有layout属性是必须的
2.include标签若指定了ID属性,而你的layout也定义了ID,则你的layout的ID会被覆盖 

3 在include标签中所有的android:layout_*都是有效的。

但前提是必须要写layout_width和layout_height两个属性,否则无效 。

看一个例子:

main.xml

  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/other" />  
  10.   
  11. </LinearLayout>  
<?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:id="@+id/include"
        layout="@layout/other" />

</LinearLayout>


include要引用的那个xml:other.xml

  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.     <ImageView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:src="@drawable/free_bg_small_3" />  
  11.   
  12. </LinearLayout>  
<?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" >

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/free_bg_small_3" />

</LinearLayout>

IncludeActivity.java

  1. package xiaosi.include;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. public class IncludeActivity extends Activity {  
  7.     /** Called when the activity is first created. */  
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.main);  
  12.     }  
  13. }  
package xiaosi.include;

import android.app.Activity;
import android.os.Bundle;

public class IncludeActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值