android 学习Layout布局的使用
2015-11-30 21:20
141 查看
android常用布局
2,主布局main.xml
2,主布局main.xml
2,主布局main.xml
3,MainActivity
LinearLayout(线性布局) 线性的垂直的水平的 RelativeLaytout(相对布局) 最灵活的 TableLayout(表格布局) 使用GridView代替 AbsoluteLayout(绝对布局) 最好不要使用绝对坐标 Framelayout(帧布局) 布局叠加时使用比如视频缓冲的环形滚动条
使用频次 Absolute<Table<Frame<Linear<Relative
android布局原则 (1)尽量多的使用线性布局和相对布局不要使用绝对布局 (2)在布局层次一样的情况下,建议使用线性布局代替相对布局因为线性布局性能稍高一点 (3)将可复用的组件抽取出来,并通过include标签使用 (4)使用viewstub标签加载一些不常用的布局(暂时不使用的) (5)使用merge标签减少标签的嵌套层次
<include/>的使用 作用:将公用的组件抽取出来单独一个xml文件,然后使用include标签导入公共布局 效果:提高ui的制作和复用效率 通过findViewById也可以找到view因为通过include实际上是将自布局直接包含进公共布局当中 1,子布局title.xml
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"android:layout_height="30dp" android:background="#243821"> </LinearLayout>
2,主布局main.xml
<LinearLayout android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical"> <includelayout="@layout/title" ></include>
</LinearLayout>
使用merge合并ui布局 作用:合并ui布局,使用该布局能降低ui布局的嵌套层次 场景1;布局根节点是FrameLayout且不需要设置backgroud和padding等属性,可以用merge代替 场景2:某布局作为自布局被其他布局include时,使用merge当做该布局的顶接点,这样再被引入时, 顶接点会自动被忽略 1,子布局progress.xml
<?xmlversion="1.0"encoding="utf-8"?> <mergexmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"android:layout_height="match_parent"> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/progress" android:layout_gravity="center"/> </merge>
2,主布局main.xml
<LinearLayout android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical"> <includelayout="@layout/title" ></include>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<includelayout="@layout/progress"/>
</FrameLayout>
</LinearLayout>
使用viewstub惰性加载 作用:viewstub标签同incude标签一样可以用来引入一个外部的布局,不同的是viewstub引入的布局默认不会扩张, 既不会占用显示也不会占用位置,从而在解析laytou时,节省cpu和内存 1,子布局common.xml
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content"android:layout_height="wrap_content"> <TextView android:id="@+id/text" android:text="viewstub" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
2,主布局main.xml
<LinearLayout android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical"> <includelayout="@layout/title" ></include>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<includelayout="@layout/progress"/>
</FrameLayout>
<Buttonandroid:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="按钮"
/>
<ViewStub
android:id="@+id/viewStub"
android:layout="@layout/common"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
3,MainActivity
publicclassMainActivityextendsActivity{ privateButtonbtn; privateViewStubviewStub; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn=(Button)findViewById(R.id.btn); viewStub=(ViewStub)findViewById(R.id.viewStub); btn.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ viewStub.inflate(); } }); } }
相关文章推荐
- Android textView属性说明
- 屏幕分辨率单位转换详解
- AlertDialog&ProgressDialog几种形式
- 【风马一族_Android】Android 前端内容1
- PackageManager 获取系统安装包信息
- ActivityManager 获取系统进程信息
- Android获取手机存储空间大小
- Android中通过代码获取arrays.xml文件中的数据
- 完全退出Android应用程序的四种方法
- Android项目——Spinner的使用
- Android Gallery橱窗效果
- Android 各国语言缩写及简称详细介绍
- android H264码流中的SPS获取
- android开发技巧总结(68个常用用法)
- 【Android】自定义View
- Android项目——ListView的使用
- Android安卓蓝牙开发
- Android IOS WebRTC 音视频开发总结(五三)-- 国内IM & RTC SDK列表
- AndroidStudio中使用plantUML
- LinearLayout中控件的置尾和置游问题