Android Resource Types之Layout简介
2015-11-22 21:02
435 查看
Re-using Layouts with include
Loading Views On Demand
文件位置:
编译的资源数据类型:
句法:
requestFocus,指定屏幕内的View获取焦点。
例子:
XML文件保存在:res/layout/main_activity.xml:
在Activity的onCreate()方法中加载布局文件:
将一个指定的布局文件加载到当前的布局文件中。
如果include标签指定了android:layout_*这种属性,那么要求android:layout_width和android:layout_height必须存在,否则其他的android:layout_*形式的属性无法生效。
merge标签
一般和include标签一起使用从而减少布局的层级。
例如,当前布局是一个竖直方向的LinearLayout,这个时候如果被包含的布局文件中也采用竖直方向的LinearLayout,那么显然被包含的不文件中的LinearLayout是多余的,通过merge标签就可以去掉多余的那一层LinearLayout。
例子:
ViewStub
ViewStub继承了View,它非常轻量级且高/宽都是0,因此本身不参与任何的布局和绘制的过程。ViewStub的意义在于按需加载所需的布局文件,在实际开发中,有很多布局文件在正常情况下不会显示,比如网络异常时的界面,这个时候就没必要在整个界面初始化的时候将其加载进来,通过ViewStub就可以做到在使用的时候再加载,提高了程序初始化时的性能。
当ViewStub通过setVisibility或者inflate方法加载后,ViewStub就会被它内部的布局替换掉,这个时候ViewStub就不再是整个布局结构中的一部分了。
例子:
定义一个ViewStub:
加载ViewStub:
Loading Views On Demand
1.Layout Resource
布局文件定义了Activity中用户界面的结构或用户界面的组件。文件位置:
res/layout/filename.xml
编译的资源数据类型:
View或View的子类
句法:
<?xml version="1.0" encoding="utf-8"?> <ViewGroup xmlns:android="http://schemas.android.com/apk/res/android" android:id="@[+][package:]id/resource_name" android:layout_height=["dimension" | "fill_parent" | "wrap_content"] android:layout_width=["dimension" | "fill_parent" | "wrap_content"] [ViewGroup-specific attributes] > <View android:id="@[+][package:]id/resource_name" android:layout_height=["dimension" | "fill_parent" | "wrap_content"] android:layout_width=["dimension" | "fill_parent" | "wrap_content"] [View-specific attributes] > <requestFocus/> </View> <ViewGroup > <View /> </ViewGroup> <include layout="@layout/layout_resource"/> </ViewGroup>
requestFocus,指定屏幕内的View获取焦点。
例子:
XML文件保存在:res/layout/main_activity.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" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, I am a TextView" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, I am a Button" /> </LinearLayout>
在Activity的onCreate()方法中加载布局文件:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); }
2.布局优化
include标签将一个指定的布局文件加载到当前的布局文件中。
如果include标签指定了android:layout_*这种属性,那么要求android:layout_width和android:layout_height必须存在,否则其他的android:layout_*形式的属性无法生效。
merge标签
一般和include标签一起使用从而减少布局的层级。
例如,当前布局是一个竖直方向的LinearLayout,这个时候如果被包含的布局文件中也采用竖直方向的LinearLayout,那么显然被包含的不文件中的LinearLayout是多余的,通过merge标签就可以去掉多余的那一层LinearLayout。
例子:
<merge xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/add"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/delete"/> </merge>
ViewStub
ViewStub继承了View,它非常轻量级且高/宽都是0,因此本身不参与任何的布局和绘制的过程。ViewStub的意义在于按需加载所需的布局文件,在实际开发中,有很多布局文件在正常情况下不会显示,比如网络异常时的界面,这个时候就没必要在整个界面初始化的时候将其加载进来,通过ViewStub就可以做到在使用的时候再加载,提高了程序初始化时的性能。
当ViewStub通过setVisibility或者inflate方法加载后,ViewStub就会被它内部的布局替换掉,这个时候ViewStub就不再是整个布局结构中的一部分了。
例子:
定义一个ViewStub:
<ViewStub android:id="@+id/stub_import" android:inflatedId="@+id/panel_import" android:layout="@layout/progress_overlay" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" />
加载ViewStub:
((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE); // or View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();
相关文章推荐
- Android学习方法路线
- Android-Material Design布局之第一曲
- Android4.4实现状态栏沉浸
- Android margin 与 padding的区别
- Android中SQLite数据库查看及导入导出
- Android官方文档阅读之旅——Introduction to Android
- Android 和 iOS 图片资源对标
- Android Volley完全解析(一),初识Volley的基本用法
- Android数据库--Sqlcipher的使用(二)
- android 4.4 Launcher2源码分析--Workspace滑动
- Android Studio安装与使用最详细的图解教程
- 作百度地图定位时,location.getAddrStr()获取到的值一直为空的问题
- Android开发手记(31) 使用MediaRecorder录音
- Android Crash监测工具Bugly初体验
- android开发总的:activity全屏设置,主题设置,透明度设置及模式设置等基础知识
- Android日志工具Log(android.util.Log)
- Android之开发常用小功能(持续更新中。。。)
- Android TextView跑马灯效果与设置文字阴影
- Android平台关于时间和日期的相关类和方法(Date/Time)
- Android——我的第一个Android程序(Hello world!)