Android开发中include控件用法分析

 更新时间:2016年10月24日 10:12:12   作者:pku_android  
这篇文章主要介绍了Android开发中include控件用法,结合实例形式分析了Android界面布局中include控件的使用技巧,需要的朋友可以参考下

本文实例讲述了Android开发中include控件用法。分享给大家供大家参考,具体如下:

我们知道,基于Android系统的应用程序的开发,界面设计是非常重要的,它关系着用户体验的好坏。一个好的界面设计,不是用一个xml布局就可以搞定的。当一个activity中的控件非常多的时候,所有的布局文件都放在一个xml文件中,很容易想象那是多么糟糕的事情!笔者通过自身的经历,用include控件来解决这个问题,下面是一个小例子,仅仅实现的是布局,没有响应代码的设计。

user.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="horizontal" >
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="用户名: " />
  <EditText
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:id="@+id/userName"
    android:hint="请输入用户名"
    />
</LinearLayout>

passwd.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="horizontal" >
   <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="密   码:" />
  <EditText
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:id="@+id/passWd"
    android:hint="请输入密码"
    />
</LinearLayout>

login.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"
  android:orientation="horizontal" >
  <Button
    android:layout_width="80dp"
    android:layout_height="wrap_content"
    android:id="@+id/bt"
    android:hint="确定"
    />
  <Button
    android:layout_width="80dp"
    android:layout_height="wrap_content"
    android:id="@+id/reset"
    android:layout_toRightOf="@id/bt"
    android:hint="重置"
    />
</RelativeLayout>

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"
  android:layout_alignParentBottom="true">
<RelativeLayout
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:layout_alignParentBottom="true">
 <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/head"
  android:layout_alignParentTop="true">
   <include
   android:layout_width="fill_parent"
   layout="@layout/user">
   </include>
 </LinearLayout>
 <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/middle"
  android:layout_below="@id/head"
  android:layout_alignParentLeft="true">
   <include
   android:layout_width="fill_parent"
   layout="@layout/passwd">
   </include>
  </LinearLayout>
  <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/foot"
  android:layout_below="@id/middle"
  android:layout_alignParentRight="true">
   <include
   android:layout_width="fill_parent"
   layout="@layout/login">
   </include>
   </LinearLayout>
</RelativeLayout>
</LinearLayout>

程序运行结果如下:

如果在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"
  android:layout_alignParentBottom="true">
   <include
   android:layout_width="fill_parent"
   layout="@layout/user">
   </include>
   <include
   android:layout_width="fill_parent"
   layout="@layout/passwd">
   </include>
   <include
   android:layout_width="fill_parent"
   layout="@layout/login">
   </include>
</LinearLayout>

那么该情况下的运行结果如下:

很显然运行结果与预期不符,接下来的四个控件出不来,为什么呢?(想必大家在做实验的时候,肯定遇到过这个问题!)

其实关键的地方是main文件中对各个xml的布局,没有相应的布局,结果是非常惨的,大家可以根据我的代码在修改下相应的布局,体会下main中布局的重要性!

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android视图View技巧总结》、《Android图形与图像处理技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android多媒体操作技巧汇总(音频,视频,录音等)》、《Android基本组件用法总结》、《Android布局layout技巧总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

相关文章

  • Android音频开发之音频采集的实现示例

    Android音频开发之音频采集的实现示例

    本篇文章主要介绍了Android音频开发之音频采集的实现示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-04-04
  • Android 登录Web 时对cookie 处理

    Android 登录Web 时对cookie 处理

    本文主要介绍 Android登录web时对cookie的处理方法,这里cookie 的读写做了详细介绍,并附有代码进行讲解,希望能帮到有需要的同学
    2016-07-07
  • Android实现调用摄像头和相册的方法

    Android实现调用摄像头和相册的方法

    这篇文章主要为大家详细介绍了Android实现调用摄像头和相册的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-04-04
  • android getActivity.findViewById获取ListView 返回NULL的方法

    android getActivity.findViewById获取ListView 返回NULL的方法

    下面小编就为大家带来一篇android getActivity.findViewById获取ListView 返回NULL的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-11-11
  • Android绘图之Paint的使用方法详解

    Android绘图之Paint的使用方法详解

    这篇文章主要给大家介绍了关于Android绘图之Paint使用的相关资料,文中通过示例代码介绍的非常详细,并给大家介绍了DrawText 基线确定的方法,需要的朋友可以参考借鉴,下面随着小编来一些学习学习吧。
    2017-11-11
  • Android自定义View之酷炫数字圆环

    Android自定义View之酷炫数字圆环

    这篇文章主要为大家详细介绍了Android自定义View之酷炫数字圆环,实现效果很酷,,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-01-01
  • Android性能优化之利用强大的LeakCanary检测内存泄漏及解决办法

    Android性能优化之利用强大的LeakCanary检测内存泄漏及解决办法

    本篇文章主要介绍了Android性能优化之利用LeakCanary检测内存泄漏及解决办法,有兴趣的同学可以了解一下。
    2016-11-11
  • Android编程实现带有单选按钮和复选按钮的dialog功能示例

    Android编程实现带有单选按钮和复选按钮的dialog功能示例

    这篇文章主要介绍了Android编程实现带有单选按钮和复选按钮的dialog功能,结合具体实例形式分析了Android实现带有单选按钮的dialog对话框及带有复选按钮的dialog对话框相关操作技巧,需要的朋友可以参考下
    2017-09-09
  • Android设备上非root的抓包实现方法(Tcpdump方法)

    Android设备上非root的抓包实现方法(Tcpdump方法)

    通常我们在Android应用中执行某个命令时会使用“Runtime.getRuntime().exec("命令路径")”这种方式,但是当我们执行抓包操作时,使用这条命令无论如何都不行,通过下面代码打印结果发现,该命令一定要在root权限下才能执行,具体实现思路,请参考本教程
    2016-11-11
  • Handler与Android多线程详解

    Handler与Android多线程详解

    一开始,相信很多人都以为myThread中的run()方法会在一个新的线程中运行,但事实并非如此。以下代码中的handler并没有调用线程myThread的start()方法,而是直接调用了run()方法,这也就意味着实际上并没有创建一个新的线程,只是在当前线程中调用run()方法而已
    2013-10-10

最新评论