xml布局内容总结(四)--Android
2015-08-08 17:04
441 查看
(1)对于xml编写界面较复杂的情况下,使用include会使得编写和查看更清楚
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include android:id="@+id/hi1" layout="@layout/health_info1" />
<include android:id="@+id/hi2" layout="@layout/health_info2" />
<include android:id="@+id/hi3" layout="@layout/health_info3" />
<include android:id="@+id/hi4" layout="@layout/health_info4" />
<include android:id="@+id/hi5" layout="@layout/health_info5" />
</LinearLayout>
这里health_info1到health_info5是5个布局文件,对应于界面中的5个模块,每个模块单独200行左右,如果在单独的模块中,不论是修改还是查看都比较容易,如果不适用include,整个布局文件就是1000行左右,不管是修改还是查看都比较麻烦,类似于代码中函数的调用。
CheckBox hiCbPs1;
hiCbPs1=(CheckBox)findViewById(R.id.hi1).findViewById(R.id.hicbps1);
这是代码中的用法
(2)自定义view的使用,在开发产品时,有时候会碰到一些需要自己进行绘制的view加入到界面中
添加到到xml中的用法
<com.jxj.view.BigDialView
android:id="@+id/healthstatedial"
android:layout_width="fill_parent"
android:layout_height="200dip"
android:layout_margin="5dip"
android:layout_gravity="center"
android:scaleType="centerInside"
/>
com.jxj.view 是包名 BigDialView是自定义的类名 com.jxj.view.BigDialView是完整的路径
这里经常容易出错的问题就是xml中找不到该类,原因一般是没有定义这个类,还是就是包名或类名写错导致xml找不到
(3)这个是在LoonAndroid框架中看到的,觉得挺好用
基本所有的控件都会用到这两条语句
android:layout_width="fill_parent"
android:layout_height="wrap_content"
在LoonAndroid框架中看到了这种用法比较简单,用一条语句替换了
<RelativeLayout
android:id="@+id/top_head"
style="@style/w_full_h_wrap"
android:background="@drawable/head_back"
android:orientation="horizontal" >
等同于
<RelativeLayout
android:id="@+id/top_head"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/head_back"
android:orientation="horizontal" >
想要使用这种方法,只需在values目录下添加
themes_basic.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 全屏幕拉伸 -->
<style name="w_h_full">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
</style>
<!-- 高度和屏幕高度一样 宽度自适应 -->
<style name="w_wrap_h_full">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">fill_parent</item>
</style>
<!-- 宽度和屏幕宽度一样 高度自适应 -->
<style name="w_full_h_wrap">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
<!-- 固定自身大小 -->
<style name="w_wrap_h_wrap">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
</resources>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include android:id="@+id/hi1" layout="@layout/health_info1" />
<include android:id="@+id/hi2" layout="@layout/health_info2" />
<include android:id="@+id/hi3" layout="@layout/health_info3" />
<include android:id="@+id/hi4" layout="@layout/health_info4" />
<include android:id="@+id/hi5" layout="@layout/health_info5" />
</LinearLayout>
这里health_info1到health_info5是5个布局文件,对应于界面中的5个模块,每个模块单独200行左右,如果在单独的模块中,不论是修改还是查看都比较容易,如果不适用include,整个布局文件就是1000行左右,不管是修改还是查看都比较麻烦,类似于代码中函数的调用。
CheckBox hiCbPs1;
hiCbPs1=(CheckBox)findViewById(R.id.hi1).findViewById(R.id.hicbps1);
这是代码中的用法
(2)自定义view的使用,在开发产品时,有时候会碰到一些需要自己进行绘制的view加入到界面中
添加到到xml中的用法
<com.jxj.view.BigDialView
android:id="@+id/healthstatedial"
android:layout_width="fill_parent"
android:layout_height="200dip"
android:layout_margin="5dip"
android:layout_gravity="center"
android:scaleType="centerInside"
/>
com.jxj.view 是包名 BigDialView是自定义的类名 com.jxj.view.BigDialView是完整的路径
这里经常容易出错的问题就是xml中找不到该类,原因一般是没有定义这个类,还是就是包名或类名写错导致xml找不到
(3)这个是在LoonAndroid框架中看到的,觉得挺好用
基本所有的控件都会用到这两条语句
android:layout_width="fill_parent"
android:layout_height="wrap_content"
在LoonAndroid框架中看到了这种用法比较简单,用一条语句替换了
<RelativeLayout
android:id="@+id/top_head"
style="@style/w_full_h_wrap"
android:background="@drawable/head_back"
android:orientation="horizontal" >
等同于
<RelativeLayout
android:id="@+id/top_head"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/head_back"
android:orientation="horizontal" >
想要使用这种方法,只需在values目录下添加
themes_basic.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 全屏幕拉伸 -->
<style name="w_h_full">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
</style>
<!-- 高度和屏幕高度一样 宽度自适应 -->
<style name="w_wrap_h_full">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">fill_parent</item>
</style>
<!-- 宽度和屏幕宽度一样 高度自适应 -->
<style name="w_full_h_wrap">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
<!-- 固定自身大小 -->
<style name="w_wrap_h_wrap">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
</resources>
相关文章推荐
- Android studio 快捷键分享(Mac osx 10.5+)
- android用户界面之GridView教程实例汇总
- 【Android测试】【第七节】Monkey——源码浅谈
- Android数据手册01:Android颜色码制表
- Android四大组件之BroadCast(续)
- Android widget 之RemoteView
- Android搜索框(保存历史记录和自动匹配)带有ScrollView嵌套ListView阻尼效果
- IntentService与Service用法区别
- android kl文件
- Android性能优化典范 - 第2季
- Android ANR产生原因和解决办法
- android中的任务栈和启动模式的总结
- Android之GridView横向左右滚动
- Android EventBus现实 听说你out该
- Android性能优化典范
- android服务的总结
- android DDMS 导出files文件夹下的容时报错
- 一个Android Socket的例子
- GitHub 优秀的 Android 开源项目
- Android之线程池深度剖析