兔兔助手官网-win7如何打开注册表
![layout_weight](/uploads/image/0631.jpg)
2023年4月6日发(作者:excel 换行)
Android布局实验(LinearLayout,RelativeLayout,Table。。。
Android布局实验
布局定义⽤户界⾯的视觉结构,如Activity或应⽤⼩部件的UI。您可以通过两种⽅式声明布局:
在XML中声明UI元素。Android提供了对应于View类及其⼦类的简明XML词汇,如⽤于⼩部件和布局的词汇;
运⾏时实例化布局元素。您的应⽤可以通过编程创建View对象和ViewGroup对象(并操纵其属性)。
Android框架让您可以灵活地使⽤以下⼀种或两种⽅法来声明和管理应⽤的UI。例如,您可以在XML中声明应⽤的默认布局,包括将出
现在布局中的屏幕元素及其属性。然后,您可以在应⽤中添加可在运⾏时修改屏幕对象(包括那些已在XML中声明的对象)状态的代码。
在XML中声明UI的优点在于,您可以更好地将应⽤的外观与控制应⽤⾏为的代码隔离。您的UI描述位于应⽤代码外部,这意味着您在修
改或调整描述时⽆需修改您的源代码并重新编译。例如,您可以创建适⽤于不同屏幕⽅向、不同设备屏幕尺⼨和不同语⾔的XML布局。此
外,在XML中声明布局还能更轻松地显⽰UI的结构,从⽽简化问题调试过程。因此,本⽂将侧重于⽰范如何在XML中声明布局。如果
您对在运⾏时实例化View对象感兴趣,请参阅ViewGroup类和View类的参考资料。下⾯来简单介绍
LInearLayout,RelativeLayout,TableLayout布局
Layout布局(线性布局)
LinearLayout是⼀个试图组,⽤于使所有⾃试图在单个⽅向(垂直或⽔平)保持对齐,可以使⽤android:orientation属性来指定布局⽅向
这⾥需要注意的是LinearLayout中⼦控件或⼦布局的属性设置,如果LinearLayout的排列⽅向是horizontal,内部的控件就绝对不能将宽度
指定为match_parent,因为这样的话的单独⼀个控件就会将整个⽔平⽅向占满,其他的控件就没有可放置的位置了。同样的道理,如果
LinearLayout的排列⽅向是vertical,内部的控件就不能将⾼度指定为match_parent。想对⼦控件的位置更好的控制,LinearLayout也⽀
持⼦视图的权重划分android:layout_weight以及android:layout_gravity属性。
android:layout_weight:此属性根据视图应在屏幕上占据的控件量向视图分配“重要性的值”,值越⼤,所占的屏幕空间越⼤。⼦视图
指定权重值后,系统会按照⼦视图声明的权重值的⽐例,将视图组中的剩余空间分配给⼦视图。默认权重为0。举个例⼦,如果是在⽔
平的LinearLayout中有三个⼦空间,他们的权重都为1时,每个空间的width的长度为整个屏幕宽度的三分之⼀,如果第三个空间的权
重为2,那么,它将获得总剩余宽度的⼀般,其他两个均享剩下的宽度
android:layout_gravity:此属性是⽤来指定控件在布局中的对齐⽅式,常⽤的取值有:
值说明
top对象与其容器的顶部对齐,不改变其⼤⼩
bottom对象与其容器的底部对齐,不改变其⼤⼩
left对象与其容器的左侧对齐,不改变其⼤⼩
right对象与其容器的右侧对齐,不改变其⼤⼩
center_vertical垂直对其⽅式:垂直⽅向上居中对齐,不改变其⼤⼩
center_horizontal⽔平对其⽅式:⽔平⽅向上居中对齐,不改变其⼤⼩
center对象横纵居中,不改变其⼤⼩
fill_vertical必要的时候增加对象的纵向⼤⼩,以完全充满其容器.垂直⽅向填充
fill_horizontal必要的时候增加对象的纵向⼤⼩,以完全充满其容器.垂直⽅向填充
fill必要的时候增加对象的横纵向⼤⼩,以完全充满容器
下⾯贴上实现效果图与代码
效果图为:
<?xmlversion="1.0"encoding="utf-8"?>
android:orientation="vertical"android:layout_width="match_parent"
android:layout_height="match_parent">
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:textSize="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="One,One"
/>
android:textSize="10dp"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="One,Two"
/>
android:textSize="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="One,Three"
/>
android:textSize="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="One,Four"/>
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:textSize="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Two,One"/>
android:textSize="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Two,Two"/>
android:textSize="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Two,Three"/>
android:textSize="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Two,Four"/>
android:textSize="10dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:textSize="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Three,One"/>
android:textSize="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Three,Two"/>
android:layout_weight="1"
android:textSize="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Three,Three"/>
android:textSize="10dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Three,Four"/>
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:textSize="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Four,One"
/>
android:textSize="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
veLayout布局(相对布局)
RelativeLayout⼜称作相对布局,也是⼀种⾮常常⽤的布局。和LInearLayout的排列规则不同的是,RelativeLayout显得更加随意⼀些,
它可以通过相对定位的⽅式让控件出现在布局的任何位置。RelativeLayout中的属性很多,但是可以归纳记忆,下⾯列出常⽤的属性
margin与padding区别
padding是站在⽗view的⾓度,是⾃⼰的内容与其⽗控件的边之间的距离。margin则是站在⾃⼰的⾓度描述问题,⾃⼰与旁边的某个组件
的距离,如果同⼀级只有⼀个view,那么它的效果基本上就和padding⼀样了
layout_alignParentEnd与layout_alignParentStart所表⽰的是与⽗布局的左边与右边对齐(⼤部分的国家是从左往右开始,少部分国家
为从右往左)
实现的效果为
android:layout_height="wrap_content"
android:text="Four,Two"
/>
android:layout_weight="1"
android:textSize="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Four,Three"
/>
android:textSize="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Four,Four"
/>
android:layout_above="@id/xxx"--将控件置于给定ID控件之上
android:layout_below="@id/xxx"--将控件置于给定ID控件之下
android:layout_toLeftOf="@id/xxx"--将控件的右边缘和给定ID控件的左边缘对齐
android:layout_toRightOf="@id/xxx"--将控件的左边缘和给定ID控件的右边缘对齐
android:layout_alignLeft="@id/xxx"--将控件的左边缘和给定ID控件的左边缘对齐
android:layout_alignTop="@id/xxx"--将控件的上边缘和给定ID控件的上边缘对齐
android:layout_alignRight="@id/xxx"--将控件的右边缘和给定ID控件的右边缘对齐
android:layout_alignBottom="@id/xxx"--将控件的底边缘和给定ID控件的底边缘对齐
android:layout_alignParentLeft="true"--将控件的左边缘和⽗控件的左边缘对齐
android:layout_alignParentTop="true"--将控件的上边缘和⽗控件的上边缘对齐
android:layout_alignParentRight="true"--将控件的右边缘和⽗控件的右边缘对齐
android:layout_alignParentBottom="true"--将控件的底边缘和⽗控件的底边缘对齐
android:layout_centerInParent="true"--将控件置于⽗控件的中⼼位置
android:layout_centerHorizontal="true"--将控件置于⽔平⽅向的中⼼位置
android:layout_centerVertical="true"--将控件置于垂直⽅向的中⼼位置
代码为
<?xmlversion="1.0"encoding="utf-8"?>
android:layout_width="match_parent"android:layout_height="match_parent">
android:id="@+id/red"
android:layout_width="80dp"
android:layout_height="80dp"
android:textSize="15sp"
android:gravity="center"
android:text="RED"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#FF0000"
/>
android:id="@+id/orange"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#FFB90F"
android:gravity="center"
android:text="ORANGE"
android:textSize="15sp"/>
android:id="@+id/yellow"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:background="#FFFF00"
android:gravity="center"
android:text="YELLOW"
android:textSize="15sp"/>
ayout布局(表格布局)
TableLayout以⾏和列的形式对控件进⾏管理,每⼀⾏为⼀个TableRow对象,每加⼊⼀个TableRow就表⽰添加⼀⾏,然后在TableRow中
每添加⼀个控件,就表⽰在该⾏中加⼊了⼀列,TableRow中的控件是不能指定宽度的。除了添加TableRow,也可以直接添加View控件,⼀
个View控件占⼀⾏。
以下为常⽤属性
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignTop="@+id/BLUE"
android:layout_marginEnd="32dp"
android:layout_toStartOf="@+id/BLUE"
android:background="#00FF00"
android:gravity="center"
android:text="GREEN"
android:id="@+id/green"/>
android:id="@+id/BLUE"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignStart="@+id/orange"
android:layout_below="@+id/orange"
android:layout_marginTop="100dp"
android:background="#0000FF"
android:gravity="center"
android:text="BLUE"
android:textColor="#FFFFFF"/>
android:id="@+id/indigo"
android:layout_width="80dp"
android:layout_height="80dp"
android:textColor="#FFFFFF"
android:layout_alignTop="@+id/BLUE"
android:layout_marginStart="34dp"
android:layout_toEndOf="@+id/BLUE"
android:background="#4B0082"
android:gravity="center"
android:text="INDIGO"/>
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentStart="true"
android:layout_below="@+id/BLUE"
android:layout_marginTop="81dp"
android:background="#EF85EF"
android:gravity="center"
android:text="VIOLET"/>
实现效果如下:
实现代码如下:
android:collapseColumns:设置需要被隐藏的列的序号
android:shrinkColumns:设置允许被收缩的列的列序号
android:stretchColumns:设置运⾏被拉伸的列的列序号
以上这三个属性的列号都是从0开始算的,⽐如shrinkColunmns="2",对应的是第三列!
可以设置多个,⽤逗号隔开⽐如"0,2",如果是所有列都⽣效,则⽤"*"号即可
除了这三个常⽤属性,还有两个属性,分别就是跳格⼦以及合并单元格,这和HTML中的Table类似:
android:layout_column="2":表⽰的就是跳过第⼆个,直接显⽰到第三个格⼦处,从1开始算的!
android:layout_span="4":表⽰合并4个单元格,也就说这个组件占4个单元格
<?xmlversion="1.0"encoding="utf-8"?>
android:layout_width="match_parent"android:layout_height="match_parent"
android:background="#000000"
android:stretchColumns="*">
>
android:layout_height="match_parent"
android:textSize="14sp"
android:text="Open..."
android:gravity="left"
android:textColor="#FFFFFF"/>
android:layout_height="match_parent"
android:text="Ctrl-O"
android:textSize="14sp"
android:gravity="right"
android:textColor="#FFFFFF"/>
>
android:layout_height="match_parent"
android:textSize="14sp"
android:text="Save..."
android:gravity="left"
android:textColor="#FFFFFF"/>
android:layout_height="match_parent"
android:text="Ctrl-S"
android:textSize="14sp"
android:gravity="right"
android:textColor="#FFFFFF"/>
>
android:layout_height="match_parent"
android:textSize="14sp"
android:text="SaveAs..."
android:gravity="left"
android:textColor="#FFFFFF"/>
android:layout_height="match_parent"
android:text="Ctrl-Shift-S"
android:textSize="14sp"
android:gravity="right"
android:textColor="#FFFFFF"/>
android:layout_width="match_parent"
android:background="#FFFFFF"
/>
>
android:layout_height="match_parent"
android:textSize="14sp"
android:text="XImport..."
android:gravity="left"
android:textColor="#FFFFFF"/>
android:layout_height="match_parent"
android:text="Ctrl-I"
android:textSize="14sp"
android:gravity="right"
android:textColor="#FFFFFF"/>
>
android:layout_height="match_parent"
android:textSize="14sp"
android:text="XExport..."
android:gravity="left"
android:textColor="#FFFFFF"/>
android:layout_height="match_parent"
android:text="Ctrl-E"
android:textSize="14sp"
android:gravity="right"
android:textColor="#FFFFFF"/>
android:layout_width="match_parent"
android:background="#FFFFFF"
/>
>
android:layout_height="match_parent"
android:textSize="14sp"
android:text="Quit..."
android:gravity="left"
android:textColor="#FFFFFF"/>
android:layout_height="match_parent"
android:text="Ctrl-Q"
android:textSize="14sp"
android:gravity="right"
android:textColor="#FFFFFF"/>
更多推荐
layout_weight
发布评论