您的位置:首页 > 移动开发 > Android开发

Android Resource Types之Layout简介

2015-11-22 21:02 435 查看
Re-using Layouts with include

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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: