在Android的layout样式定义中,可以使用xml文件方便的实现,有时候为了模块的复用,使用include标签可以达到此目的。
例如:   

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 tag,意思是任何android:layout_*属性都可以应用在标签中。
如果使用如下代码:         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" />                                  android:layout_below="@id/top" />         
 
发现include的otherlayout,并没有在如我们预期的在id/top这个TextView下面,而是忽略了android:layout_below属性。经过Google发现,很多人遇到类似的问题。
有解决方法是在include的外面再包一层LinearLayout,如下:         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:layout_below="@id/top" >                  < include layout="@layout/otherlayout">  
在这里找到更好的解决方法: http://stackoverflow.com/questions/2316465/how-to-get-relativelayout-working-with-merge-and-include 解答道:必须同时重载layout_width和layout_height熟悉,其他的layout_*属性才会起作用,否这都会被忽略掉。上面的例子应该写成这样:
    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
参考网址: https://developer.android.com/resources/articles/layout-tricks-reuse.html
http://stackoverflow.com/questions/2316465/how-to-get-relativelayout-working-with-merge-and-include
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"?>
                android:id="@+id/index_linear_foot"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
                    android:id="@+id/head_btn_refresh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />

2.新建一个布局文件,命名 main.xml
main.xml文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
>


注意:上面我们的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"?>
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/main_headb"
/>

那接下来我们在运行我们的实例,结果发现,代码在运行到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”

使用include实现布局(layout)复用

  假使我们遇到这么一种情况,我们需要开发一个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的文字

更多相关文章

  1. Android(安卓)productFalvors 多渠道打包方案
  2. android中沉浸式布局的设置
  3. android上如何写配置文件
  4. 菜单之二:使用xml文件定义菜单
  5. Android中sqllite存储海量数据解决办法
  6. Android点击事件的实现
  7. Android(安卓)字体大小怎么自适应不同分辨率?
  8. Android(安卓)开发相关命令集合
  9. android打包so文件到apk

随机推荐

  1. afinal快速开发框架
  2. Android(安卓)- 文件读写操作 总结
  3. textView属性
  4. 【Android手机游戏】贪吃蛇(1)
  5. android send mail
  6. Android(安卓)使用Loader示例
  7. Android中蓝牙模块的使用
  8. Android高手进阶教程(三)之----Android(
  9. android -- 蓝牙 bluetooth (一) 入门
  10. Android(安卓)关于嵌套listView时onItemC