Android下的布局
来源:互联网 发布:pc蛋蛋幸运28算法公式 编辑:程序博客网 时间:2024/06/03 09:26
控件的相对位置
需求:在屏幕上写一个十字型的button
一:通过属性控制布局和窗体的位置关系(前提:layout_width和layout_height 至少有一个为warp_content)
代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"//这里可以设置为:center left right等表现为①②③ android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="十字型button" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout><LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout></LinearLayout>
//具体表现如下①②③
二 通过设定值确定与边框的位置关系
android:layout_marginLeft指该控件距离边父控件的边距,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_marginLeft="30dip"//用数值方法设置与边缘的位置关系 android:layout_marginTop="70dip" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="十字型button" />
控件中元素的位置
android:paddingLeft指该控件内部内容,如文本距离该控件的边距。
<?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" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="控件的相对位置" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="1dip" android:text="1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="277px" android:text="2"/></LinearLayout><LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" ><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" android:text="3" /></LinearLayout></LinearLayout>
控件中内容的相对位置2
below 属性的使用
<RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="12"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="13" android:layout_alignParentRight = "true" /> <Button android:id="@+id/bt5" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="14"/> <Button android:layout_below="@id/bt5" android:layout_centerHorizontal="true"//如果没有设置居中属性的话控件会放在左边 android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="15"/> </RelativeLayout>
paddingLeft和marginLeft
android:paddingLeft="80px"按钮上设置的内容(例如图片)离按钮左边边界80个像素android:layout_marginLeft="80px"整个按钮离左边设置的内容80个像素
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <EditText android:layout_marginLeft="80dip" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请输入姓名" android:background="#ff0000" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <EditText android:paddingLeft="80dip" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请输入电话" android:background="#ff0000" /> </LinearLayout>
layout_weight
<?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="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" android:layout_weight="1" android:background="#ffccbb" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2" android:layout_weight="2" android:background="#ff0000" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3" android:layout_weight="3" android:background="#ffeeee" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4" android:layout_weight="1" android:background="#ffccbb" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="5" android:layout_weight="2" android:background="#ff0000" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="6" android:layout_weight="3" android:background="#ffeeee" /> </LinearLayout><LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="7" android:layout_weight="1" android:gravity="center_horizontal" android:background="#ffccbb" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="8" android:layout_weight="2" android:gravity="left" android:background="#ff0000" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="9" android:layout_weight="3" android:gravity="left" android:background="#ffeeee" /> </LinearLayout></LinearLayout>三个文本框的都是 layout_width=“wrap_content ”时,width显示的比例为1:2:3
layout_width=“fill_parent”时,width显示的比例是:
系统先给3个textview分配他们所要的宽度fill_parent,也就是说每一都是填满他的父控件
那么这时候的剩余空间=1个parent_width-3个parent_width=-2个parent_width (parent_width指的是屏幕宽度 )
那么第7个TextView的实际所占宽度应该=fill_parent的宽度,即parent_width + 他所占剩余空间的权重比列1/6 * 剩余空间大小(-2 parent_width)=2/3parent_width
同理第8个TextView的实际所占宽度=parent_width + 2/6*(-2parent_width)=1/3parent_width;
第9个TextView的实际所占宽度=parent_width + 3/6*(-2parent_width)=0parent_width;所以就是2:1:0的比列显示了。第三个就直接没有空间了。
- Android下的布局
- Android下的布局
- android res下的布局文件以及values下的布局文件
- (android中的动态布局)关于Android下各个布局方式里面LayoutParams的用法
- android setContentView() 找不到 R下的布局文件的解决办法。
- 关于Android下常用布局的LayoutParams的用法
- android setContentView() 找不到 R下的布局文件的解决办法
- android UI布局 与windows 下的 wpf 的比较
- Android控制布局下的控件的点击事件
- 关于Android下各个布局方式里面LayoutParams的用法
- 关于Android下各个布局方式里面LayoutParams的用法
- android 软键盘在全屏下的布局计算问题
- Android下一行平均分布图片的布局
- 请介绍下Android中常用的五种布局
- android 软键盘在全屏下的布局计算问题
- Android Studio下的应用性能优化总结--布局优化
- 关于Android下各个布局方式里面LayoutParams的用法
- 关于Android下各个布局方式里面LayoutParams的用法
- AJAX - onreadystatechange 事件
- Best Time to Buy and Sell Stock III
- mysql root密码找回
- adb配置
- Android中使用apache commons-net发送后台邮件的方法
- Android下的布局
- Java中文件的上传与下载
- angularjs directive semantic-ui pagination
- angularjs route 中的用户身份认证
- angularjs 利用 service + $broadcast 在多个controllers 之间共享数据
- 游戏服务器端编程书籍
- Java中ASC码与字符互相转换
- DedeCMS添加字段提示Invalid utf8 character string错误的解决办法
- angularjs 数据初始化