Layout--Linear与Relative

来源:互联网 发布:现在淘宝怎么收藏店铺 编辑:程序博客网 时间:2024/04/26 09:30

LinearLayout

注意:LinearLayout的orientation如果没有设置,那么默认的就是horizontal水平方向


layout_weight:

用于给一个线性布局中的诸多视图的重要度赋值

可以按照赋值来确定将要显示的比例

按准确比例显示LinearLayout内各个子控件:

如果为水平方向需设置android:layout_width="0dp",

为竖直方向需设置android:layout_height="0dp"。

在这种情况下某子个控件占用LinearLayout的比例为:本控件weight值 / LinearLayout内所有控件的weight值的和。

 

一般我们控制比重都是设置对应方向的高或宽为0dp来实现我们想的比重控制,而很少用fill_parent和wrap_content。

fill_parent和wrap_content之间的区别(orientation=“horizontal”)

    当layout_width为 fill_parent时:

   layout_height代表的是控件要尽可能大,但这个大是有限度的,即fill_parent

  在分配大小时,权重和所分配的大小成反比,本人在这里还未弄清具体的比例,不过对比图片,可以看出具体的比例并不是

weight值 /LinearLayout内所有控件的weight值的和

    当layout_width为wrap_content时:

layout_weight代表的是你的控件要优先尽可能的小,但这个小是有限度的,即wrap_content.
在分配大小时,权重和所分配的大小成正比,同上,具体比例未知


所以我们在使用weight时一定要设置

水平方向(horizontal):android:layout_width="0dp",

竖直方向(vertical):android:layout_height="0dp"。


示例样图




示例代码:

<?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" >        <!-- 在整体的LinearLayout中定义了3个LinearLayout方向为vertical,又因为height为fill_parent,所以权重和实际的大小成反比 -->        <!-- 每一个控件的height都为fill_parent,所以权重和实际大小成反比 ,具体比例未知-->            <LinearLayout            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:layout_weight="1" >            <Button                android:layout_width="fill_parent"                android:layout_height="fill_parent"                android:layout_weight="2"                android:background="#00AAC0" />            <Button                android:layout_width="fill_parent"                android:layout_height="fill_parent"                android:layout_weight="2"                android:background="#CCdd00" />            <Button                android:layout_width="fill_parent"                android:layout_height="fill_parent"                android:layout_weight="1"                android:background="#0000FF" />        </LinearLayout>        <!--LinearLayout默认为horizontal,又因为每个控件width为wrap_content,所以权重和实际大小成正比        具体比例未知  -->                <LinearLayout            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:layout_weight="2" >            <Button                android:layout_width="wrap_content"                android:layout_height="fill_parent"                android:layout_weight="1"                android:background="#FFAA00" />            <Button                android:layout_width="wrap_content"                android:layout_height="fill_parent"                android:layout_weight="3"                android:background="#CCdd00" />            <Button                android:layout_width="wrap_content"                android:layout_height="fill_parent"                android:layout_weight="1"                android:background="#0000FF" />        </LinearLayout>        <!-- 每个控件的width为0,所以每个控件的权重与实际大小由weight的比重确定,即成正比 :weight/weight总和-->        <LinearLayout            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:orientation="horizontal"            android:layout_weight="2"             >            <Button                android:layout_width="0dp"                android:layout_height="fill_parent"                android:layout_weight="1"                android:background="#00FF00" />            <Button                android:layout_width="0dp"                android:layout_height="fill_parent"                android:layout_weight="3"                android:background="#CCdd00" />            <Button                android:layout_width="0dp"                android:layout_height="fill_parent"                android:layout_weight="1"                android:background="#cc00FF" />        </LinearLayout></LinearLayout>

注意:

如果我们要控制显示比重,那么我们一般要求各个等级的元素的控制方式必须一致,比如多个button控件处于同一等级来控制比重布局,

那么如果设置的是android:layout_width="0dp"这样的方式来控制,就必须把所有的等级控件都用android:layout_width="0dp"来控制,而不能

有的用android:layout_width="0dp",而还有的用android:layout_width=" fill_parent ",这样就会出现混乱了


RelativeLayout

RelativeLayout的各种属性

android:layout_above将该控件的底部至于给定ID的控件之上

android:layout_below 将该控件的顶部至于给定ID的控件之下

android:layout_toLeftOf 将该控件的右边缘和给定ID的控件的左边缘对齐

android:layout_toRightOf 将该控件的左边缘和给定ID的控件的右边缘对齐


android:layout_alignBaseline 该控件的baseline和给定ID的控件的baseline对齐

android:layout_alignBottom 将该控件的底部边缘与给定ID控件的底部边缘

android:layout_alignLeft 将该控件的左边缘与给定ID控件的左边缘对齐

android:layout_alignRight 将该控件的右边缘与给定ID控件的右边缘对齐

android:layout_alignTop 将给定控件的顶部边缘与给定ID控件的顶部对齐


android:alignParentBottom 如果该值为true,则将该控件的底部和父控件的底部对齐

android:layout_alignParentLeft如果该值为true,则将该控件的左边与父控件的左边对齐

android:layout_alignParentRight  如果该值为true,则将该控件的右边与父控件的右边对齐

android:layout_alignParentTop 如果该值为true,则将空间的顶部与父控件的顶部对齐


android:layout_centerHorizontal 如果值为true,该控件将被至于水平方向的中央

android:layout_centerInParent 如果值为true,该控件将被至于父控件水平方向和垂直方向的中央

android:layout_centerVertical 如果值为true,该控件将被至于垂直方向的中央


示例图



示例代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:padding="10px" >    <TextView android:id="@+id/label"               android:layout_width="fill_parent"               android:layout_height="wrap_content"               android:text="Type here:" />    <EditText android:id="@+id/entry"               android:layout_width="fill_parent"               android:layout_height="wrap_content"               android:background="@android:drawable/editbox_background"              android:layout_below="@id/label" />      <Button android:id="@+id/ok"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_below="@id/entry"            android:layout_alignParentRight="true"            android:layout_marginLeft="10px"            android:text="OK" />    <Button android:layout_width="wrap_content"             android:layout_height="wrap_content"            android:layout_toLeftOf="@id/ok"            android:layout_alignTop="@id/ok"            android:text="Cancel" /></RelativeLayout>


原创粉丝点击