Android学习之六:使用Container

1.LinearLayout:包含的子控件是以列或行的方式进行排列。

  • Orientation:是以什么方式排列,值有:horizontalvertical。也可以在java代码中调用setOrientation(LinearLayout.HORIZONTAL)。

    <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
        android:orientation=”vertical”
        android:layout_width=”fill_parent”
        android:layout_height=”fill_parent”
        > </LinearLayout>

  • 填充模式:使用android:layout_widthandroid:layout_height来设置宽度和高度,可以使用具体值来指定宽度,例如125px;也可以使用fill_parent或者 wrap_content来指定是填充父控件,或者按自身内容。

    <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
        android:orientation=”vertical”
        android:layout_width=”fill_parent”
        android:layout_height=”wrap_content”
        >

    </LinearLayout>

  • Weight:用于给一个线性布局中的诸多视图的重要度赋值。所有的视图都有一个layout_weight值,默认为零,意思是需要显示 多大的视图就占据多大的屏幕空 间。若赋一个高于零的值,则将父视图中的可用空间分割,分割大小具体取决于每一个视图的layout_weight值以及该值在当前屏幕布局的整体 layout_weight值和在其它视图屏幕布局的layout_weight值中所占的比率而定。 举个例子:比如说我们在 水平方向上有一个文本标签和两个文本编辑元素。该文本标签并无指定layout_weight值,所以它将占据需要提供的最少空间。如果两个文本编辑元素每一个的layout_weight值都设置为1,则两者平分在父视图布局剩余的宽度(因为我们声明这两者的重要度相等)。如果两个文本编辑元素其中第一个的layout_weight值设置为1,而第二个的设置为2,则剩余空间的三分之二分给第一个,三分之一分给第二个(数值越小,重要度越高)注意设置时,要想 layout_weight起作用,其宽度或高度必须设置为”fill_parent”,注意下面代码绿色标注部分。

    <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
        android:orientation=”horizontal”
        android:layout_width=”fill_parent”
        android:layout_height=”fill_parent”
        >
    <TextView android:layout_width=”fill_parent”
              android:layout_height=”wrap_content”
              android:text=”hello”
             android:layout_weight=”1″
    />
    <TextView android:layout_width=”fill_parent”
              android:layout_height=”wrap_content”
              android:text=”test”
              android:layout_weight=”2″
              android:background=”#ffffffff”
    />
    </LinearLayout>

    2010-10-26-01.jpg?psid=1

我们看到运行效果,第一个显示”hello”的设置android:layout_weight=”1”,它占了三分之二的空间,而另外一个设置为”2”,它占了三分之一到空间。假如上面的一个设置了layout_weight值,一个没有设置,没有设置值的控件将完全占据该空间,设置值的控件将不会被显示出来。setGravity(Gravity.LEFT)

  • layout_gravity:子控件以什么方式对齐。

    <TextView android:layout_width=”wrap_content”
              android:layout_height=”wrap_content”
              android:text=”test”
              android:background=”#ffffffff”
             android:layout_gravity=”right”
    />

    2010-10-26-02.jpg?psid=1

  • padding:控件中的值距离控件边缘的值。android:padding、android:paddingLeft、android:paddingRight、android:paddingTop、android:paddingBottom。2010-10-26-03.jpg?psid=1

<TextView android:layout_width=”fill_parent”
          android:layout_height=”wrap_content”
          android:text=”test1″
          android:background=”#ffffffff”         
/>
<EditText android:layout_width=”wrap_content”
          android:layout_height=”wrap_content”
          android:text=”hello world”
          android:padding=”30px”    // 控件中值距离边框的距离
          android:layout_margin=”20px”   //控件距离四周控件的距离
/>

2010-10-26-04.jpg?psid=1

2.RelativeLayout:根据 Container中其它widgets来进行布局。

标识一个Widgets在其Container中的位置,我们有下面的属性参数可以用,这些参数的值可以使true或者false:

  • android:layout_alignParentTop:表示widget的顶部和Container的顶部重合。
  • android:layout_alignParentBottom:表示widget的底部和Container的底部重合。
  • android:layout_alignParentLeft:表示widget的左边和Container的左边重合。
  • android:layout_alignParentRight:表示widget的右边和Container的右边重合。
  • android:layout_centerHorizontal:表示widget处于Container水平方向上的中间。
  • android:layout_centerVertical:表示widget处于Container垂直方向上的中间。
  • android:layout_centerInParent:表示widget处于Container平面上的正中间。

标识同一个Container中的不同的widgets之间的相对位置时,可有用如下的属性参数,参数值是其他widgets的应用。相对位置上的widgets的引用的设定如下:首先必须给所有的被引用的widgets赋予标示符,通过 android:id的属性,格式如@+id/widget1,然后在其他widget引用的时候使用同样的标示符,格式如@id/widget1,例:如果widget A标识为@+id/widget A,则widget B在其属性里面通过@id/widget A引用widget A。

  • android:layout_above:表示该widget必须位于参数值标识的widget的上方。
  • android:layout_below:表示该widget必须位于参数值标识的widget的下方。
  • android:layout_toLeftOf:表示该widget必须位于参数值标识的widget的左方。
  • android:layout_toRightOf:表示该widget必须位于参数值标识的widget的右方。
  • android:layout_alignTop:表示该widget的顶部必须与参数值标识的widget的顶部重合。
  • android:layout_alignBottom:表示该widget的底部必须与参数值标识的widget的底部重合。
  • android:layout_alignLeft:表示该widget的左边必须与参数值标识的widget的左边重合。
  • android:layout_alignRight:表示该widget的右边必须与参数值标识的widget的右边重合。
  • android:layout_alignBaseline:大概相当于水平中心线对齐。

下面看xml示例代码:

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
    android:orientation=”vertical”
    android:layout_width=”fill_parent”
    android:layout_height=”fill_parent”
    >
<TextView android:id=”@+id/label”
          android:text=”URL:”
          android:layout_width=”wrap_content”
          android:layout_height=”wrap_content”
          android:layout_marginTop=”15px”
/>
<EditText android:id=”@+id/entry”
          android:text=”http://www.google.com”
          android:layout_width=”fill_parent”
          android:layout_height=”wrap_content”
          android:layout_toRightOf=”@id/label”
          android:layout_marginLeft=”10px”
/>
<Button android:id=”@+id/ok”
        android:text=”OK”
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_below=”@id/entry”
        android:layout_alignRight=”@id/entry”
/>
<Button android:id=”@+id/cancel”
        android:text=”cancel”
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_toLeftOf=”@id/ok”
        android:layout_alignTop=”@id/ok”
/>
</RelativeLayout>

下面看运行效果:

2010-10-28-01.jpg?psid=1

转载于:https://www.cnblogs.com/zjmsky/archive/2010/12/07/1898815.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值