现在的位置: 首页 > 综合 > 正文

Android 布局管理器 之 RelativeLayout&FrameLayout&AbsoluteLayout

2012年07月12日 ⁄ 综合 ⁄ 共 3626字 ⁄ 字号 评论关闭

3 RelativeLayout

子控件的位置是相对兄弟控件或父控件的位置而决定的。ViewA的位置相对于ViewB来决定,要保证B出现在A之前。

属性名称 属性说明
android:layout_centerHorizontal 当前控件位于父控件的横向中间位置
android:layout_centerVertical 当前控件位于父控件的纵向中间位置
android:layout_centerParent 当前控件位于父控件的纵横向中间位置
android:layout_alignParentBottom 当前控件低端与父控件的低端对齐
android:layout_alignParentLeft 当前控件左端与父控件的左端对齐
android:layout_alignParentRight 当前控件右端与父控件的右端对齐
android:layout_alignParentTop 当前控件上端与父控件的上端对齐
android:layout_alignWithParentIfMissing 参照控件不存在或不可见时参照父控件
   
android:layout_toRightOf 使当前控件位于给出id控件的右侧
android:layout_toLeftOf  
android:layout_above  
android:layout_below  
android:layout_alignTop  
android:layout_alignBottom  
android:layout_alignLeft  
android:layout_alignRight  
   
android:layout_marginLeft 当前控件左侧的留白
android:layout_marginRight  
android:layout_marginTop  
android:layout_marginBottom  

relavitelayout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <ImageView android:id="@+id/iv01" android:background="@drawable/img01"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />
    <ImageView android:id="@+id/iv02" android:background="@drawable/img02"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_toRightOf="@id/iv01" android:layout_alignTop="@id/iv01" />
    <ImageView android:id="@+id/iv03" android:background="@drawable/img03"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_below="@id/iv01" android:layout_alignLeft="@id/iv01" />
</RelativeLayout>

image

4 FrameLayout

在FrameLayout中,子控件是通过栈来绘制的,所有后添加的子控件会被绘制在上层

属性名称 对应方法 描述
android:foreground setForeground(Drawable) 设置绘制在所有控件之上
android:foregroundGravity setForegroundGravity(int) 设置绘制在所有子控件之上内容的gravity属性

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <TextView android:id="@+id/big" android:text="大" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:textSize="60px"
        android:textColor="@drawable/white" />
    <TextView android:text="中" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:textSize="40px"
        android:textColor="@drawable/darkgray" />
    <TextView android:text="小" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:textSize="20px"
        android:textColor="@drawable/abc" />
</FrameLayout>

image

5 AbsoluteLayout

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <ImageView android:layout_height="wrap_content" android:src="@drawable/img01"
        android:id="@+id/imageView1" android:layout_width="wrap_content"
        android:layout_x="57dip" android:layout_y="41dip"></ImageView>
    <ImageView android:layout_height="wrap_content" android:src="@drawable/img02"
        android:id="@+id/imageView2" android:layout_width="wrap_content"
        android:layout_x="174dip" android:layout_y="38dip"></ImageView>
    <TextView android:layout_height="wrap_content" android:text="TextView"
        android:layout_x="98dip" android:id="@+id/textView1"
        android:layout_width="wrap_content" android:layout_y="122dip"></TextView>
    <Button android:layout_height="wrap_content" android:id="@+id/button1"
        android:layout_x="93dip" android:text="Button" android:layout_width="wrap_content"
        android:layout_y="173dip"></Button>
</AbsoluteLayout>

image

抱歉!评论已关闭.