Android布局之LinerLayout点滴
2015-12-15 12:13
633 查看
LinerLayout顾明思议,线性布局,指定VIEW只能按横向或者竖线进行依次排列。
android:orientation:vertical(竖向排列) ;horizontal(横向排列)
示例演示:
效果如下:
修改
效果:
已经实现了计算器部分界面,自己补充完整即可。
不对,第一行排列为什么不整齐呢,小伙伴们也发现了
android:layout_weight="1"
android:layout_margin="2dip"
android:layout_gravity="bottom"
layout_weight属性:将各个VIEW宽度进行平均按比例分配;
layout_margin属性:与其它VIEW之间间距设置
lyaout_gravity属性:设置在父VIEW中的对其方式,重点说明,如果LinerLayout的orientation设置的为horizontal,则属性值top bottom 有效;如果设置为vertical则left right等有效。
让我们依次看下效果吧
button1占用50%控件,button2,3分别占用25%空间。
android:orientation:vertical(竖向排列) ;horizontal(横向排列)
示例演示:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button3" /> </LinearLayout>
效果如下:
修改
android:orientation="vertical",再看下效果
</pre><img src="http://img.blog.csdn.net/20151215120612954?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /><p></p><p></p><p>有了这个属性,开始我们的UI旅程吧,写一个计算器界面,参考系统计算器</p><p></p><p></p><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="100dip" android:orientation="horizontal"> <Button android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="2" android:layout_margin="2dip" android:text="BackSpace" /> <Button android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="2dip" android:layout_gravity="center_vertical" android:text="CE" /> <Button android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="2dip" android:layout_gravity="bottom" android:text="C" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="2dip" android:text="7" /> <Button android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="2dip" android:text="8" /> <Button android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="2dip" android:text="9" /> <Button android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="2dip" android:text="/" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="2dip" android:text="4" /> <Button android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="2dip" android:text="5" /> <Button android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="2dip" android:text="6" /> <Button android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="2dip" android:text="*" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="2dip" android:text="1" /> <Button android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="2dip" android:text="2" /> <Button android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="2dip" android:text="3" /> <Button android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="2dip" android:text="-" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="2dip" android:text="0" /> <Button android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="2dip" android:text="+/-" /> <Button android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="2dip" android:text="." /> <Button android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="2dip" android:text="+" /> </LinearLayout> </LinearLayout>
效果:
已经实现了计算器部分界面,自己补充完整即可。
不对,第一行排列为什么不整齐呢,小伙伴们也发现了
android:layout_weight="1"
android:layout_margin="2dip"
android:layout_gravity="bottom"
layout_weight属性:将各个VIEW宽度进行平均按比例分配;
layout_margin属性:与其它VIEW之间间距设置
lyaout_gravity属性:设置在父VIEW中的对其方式,重点说明,如果LinerLayout的orientation设置的为horizontal,则属性值top bottom 有效;如果设置为vertical则left right等有效。
让我们依次看下效果吧
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <Button android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="2" android:text="1" /> <Button android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="2" /> <Button android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" /> </LinearLayout>
button1占用50%控件,button2,3分别占用25%空间。
相关文章推荐
- Android前置摄像头预览并检测人脸,获取人脸区域亮度
- 【Android Studio快捷键】之导入相应包声明(import packages)
- android存储之preference和SQLite
- Android:ContentProvider 随记2
- Android学习笔记(21):ImageView及其子类
- Android中WIFI开发总结(一)
- android studio svn 更改依赖包名后 报错 Gradle Configuration with name 'default' not found
- Android中关于SQLite的操作
- Android:ContentProvider 随记
- Android Studio Git .gitignore文件简洁正确的写法
- android xmlns:tools用法
- android学习的几本书:深入理解Android内核设计思想
- Android 开源项目推荐
- Android中Handler的使用
- android学习的几本书:《Android从入门到精通》目录
- Android深入浅出之Binder机制
- TextView字体加粗 ---Android基础篇
- android contentprovider的使用(二)
- Android签名
- Android 打包Jar(发布SDK)常见问题及解决方案