android中include标签的使用

转自:http://blog.sina.com.cn/s/blog_a364999b01017gxi.html

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);
这样就可以了。

时间: 2024-08-03 13:49:53

android中include标签的使用的相关文章

android中include标签使用详解

android中include标签是为了便于控件的覆用的一个很好解决方案. 但是也有一些需要注意的地方,下面是本人在项目中碰到过的一个问题,做此记录,便于以后查看. include标签用法. 1.新建一个xml文件,命名 head.xml head.xml文件内容如下: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.

Android中include标签的使用及注意事项

前言 ??include标签可以实现在一个layout中引用另一个layout的布局,这通常适合于界面布局复杂.不同界面有共用布局的APP中,比如一个APP的顶部布局.侧边栏布局.底部Tab栏布局.ListView和GridView每一项的布局等,将这些同一个APP中有多个界面用到的布局抽取出来再通过include标签引用,既可以降低layout的复杂度,又可以做到布局重用(布局有改动时只需要修改一个地方就可以了). 使用方法 ??include标签的使用很简单,只需要在布局文件中需要引用其它布

Android中 include标签的使用

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" style="@style/StyleLayoutMain" mce_style="@style/StyleLayoutMain&

Android中include标签的使用(打开引用布局,隐藏当前布局)

在开发app的时候,有时候一个布局会反复用到,可以把反复用到的布局单独写一个xml文件,什么时候用到就用includ标签引入xml 下面是我写的反复用到的一个xml,里面有2个button,一个TextView和一个ProgressBar layout_progress.xml <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas

二十二、android中application标签说明

<application> <applicationandroid:allowClearUserData=["true" | "false"]android:allowTaskReparenting=["true" | "false"]android:backupAgent="string"android:debuggable=["true" | "false

Android中include的使用

如果在程序中多次用到一部分相同的布局,可以先将这部分布局定义为一个单独的XML,然后在需要的地方通过<include>引入,如下: main.xml ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q

mybatis中&lt;include&gt;标签的作用

MyBatis中sql标签定义SQL片段,include标签引用,可以复用SQL片段 sql标签中id属性对应include标签中的refid属性.通过include标签将sql片段和原sql片段进行拼接成一个完整的sql语句进行执行. <sql id="sqlid"> res_type_id,res_type </sql> <select id="selectbyId" resultType="com.property.vo

Android中&lt;original-package&gt;标签含义

在AndroidManifest.xml中,<original-package>与<manifest package=...>中的区别:<original-package>:应用源码所在包<manifest package=...>:应用运行时的进程名,同样也是R.java所在包名因此,上述两者名称可以不同,一般不写<original-package>,但若写,则需要注意以下几点:1.若<manifest package=...>与&

Android中preference标签的使用

现在做公司任务的时候,经常会要去读Settings的源码,然后发现在xml文件中几乎全是用的preferenceScreen和preferenceCategory标签,很少有用布局和控件的,然后我就自己上网看了很多有关的资料,在此总结下. 首先在res目录下,新建一个命名为xml的文件夹,然后建立一个aaa.xml文件,选择resource标签. 先使用PreferenceCategory,代码如下 http://fl.365jia.cn/info/2277638.htmlhttp://fl.3