布局管理器

Android提供了以下5种布局管理器

相对布局管理器(RelativeLayout):通过相对定位的方式来控制组件的摆放位置

线性布局管理器(LinearLayout):是指垂直或水平方式依次摆放位置

帧布局管理器(FrameLayout):没有任何定位,默认情况下路所有的组件都会摆放左上角,

表格布局(TableLayout)

相对布局管理器


xml属性描述
android:layout_above其属性值为其他UI组件ID属性,用于指定该组件位于那个组件上方
android:layout_alignButton其属性值为其他UI组件ID属性,用于指定该组件位于那个组件下边界对齐
android:layout_alignLeft其属性值为其他UI组件ID属性,用于指定该组件位于那个组件左边界对齐
android:layout_alignRight其属性值为其他UI组件ID属性,用于指定该组件位于那个组件右边界对齐
android:layout_alignTop其属性值为其他UI组件ID属性,用于指定该组件位于那个组件上边界对齐
android:layout_alignParentBottom其属性值为boolean,用于指定该组件是否布局管理底端对齐
android:layout_alignParentLeft其属性值为boolean,用于指定该组件是否布局管理左对齐
android:layout_alignParentRight其属性值为boolean,用于指定该组件是否布局管理右对齐
android:layout_below其属性值为其他UI组件ID属性,用于指定该组件位于那个组件下边界对齐
android:layout_centerHorizontal其属性值为boolean,用于指定该组件是否布局管理水平居中的位置
android:layout_centerInParent其属性值为boolean,用于指定该组件是否布局管理中央的位置
android:layout_centerInVertical其属性值为boolean,用于指定该组件是否布局管理垂直居中的位置
android:layout_toLeftOf其属性值为其他UI组件ID属性,用于指定该组件位于那个组件的左侧
android:layout_toRightOf其属性值为其他UI组件ID属性,用于指定该组件位于那个组件的右侧

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:background="@mipmap/bg"
    >
    <!-- 添加一个居中显示的文本视图textView1 -->
    <TextView
        android:text="发现有Widget的新版本,您现在就要安装吗?"
        android:id="@+id/textView1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_centerInParent="true"
        />
    <!-- 添加一个按钮button2,该按钮与textView1的右边界对齐 -->
    <Button
        android:text="以后再说"
        android:id="@+id/button2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView1"
        />
    <!-- 添加一个在button2左侧显示的按钮button1 -->
    <Button
        android:text="现在更新"
        android:id="@+id/button1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_below="@id/textView1"
        android:layout_toLeftOf="@id/button2"
        />

</RelativeLayout>

 线性布局 LinearLayout

排列方式由 Android:orientation 属性来控制,对齐方式由android:gravity 属性来控制

android:orientation用于设置布局管理器内组件的排列方式,可选值为horizontal(水平方式) 和vertical(垂直方式) 
android:gravity

用于设置布局管理器内组件的显示位置

top,bottom,left,right,center_verical 和center_horizontal等

Android:layout_weight设置组件权重,用于设置组件占剩余空间的比例,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_vertical_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    >
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="20dp"
        android:hint="QQ号/微信号/Email"
        android:drawableLeft="@mipmap/zhanghao"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="20dp"
        android:hint="密码"
        android:drawableLeft="@mipmap/mima"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        android:textColor="#FFFFFF"
        android:background="#FF009688"/>
    <!--第四行-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录遇到问题?"
        android:gravity="center_horizontal"
        android:paddingTop="20dp"/>

</LinearLayout>

通过线性布局管理实现模拟App底部选项卡的显示效果

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"
        ></FrameLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:layout_gravity="center"
        >
        <RadioButton
            android:id="@+id/radioButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="首页"
            android:textSize="17dp"
            android:background="@android:color/holo_blue_light"
            android:gravity="center"
            android:button="@null"/>

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="联系人"
            android:background="@android:color/holo_green_light"
            android:textSize="17dp"
            android:gravity="center"
            android:button="@null"/>

        <RadioButton
            android:button="@null"
            android:textSize="17dp"
            android:gravity="center"
            android:id="@+id/radioButton3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@android:color/holo_orange_light"
            android:layout_weight="1"
            android:text="朋友圈" />

        <RadioButton
            android:id="@+id/radioButton4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="17dp"
            android:gravity="center"
            android:text="我"
            android:background="@android:color/holo_red_light"
            android:button="@null"/>

    </LinearLayout>

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--第一条信息-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        >
        <ImageView
            android:id="@+id/icon1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_margin="10dp"
            android:src="@mipmap/v_ico1"
            />
        <TextView
            android:id="@+id/name1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_toRightOf="@+id/icon1"
            android:text="小明"
            android:textColor="#576B95"
            />
        <TextView
            android:id="@+id/content1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/name1"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:layout_toRightOf="@+id/icon1"
            android:maxLines="3"
            android:text="祝我身体健康,家人万事如意"
            />
        <TextView
            android:id="@+id/time1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/content1"
            android:layout_marginTop="3dp"
            android:layout_toRightOf="@+id/icon1"
            android:text="昨天"
            android:textColor="#9A9A9A"
            />
        <ImageView
            android:id="@+id/comment1"
            android:layout_width="30dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@id/content1"
            android:src="@mipmap/comment"
            />
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/line"
            />

    </RelativeLayout>
    <!-- 分隔线 -->
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@mipmap/line" />
    <!-- 第二条信息 -->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp" >

        <ImageView
            android:id="@+id/ico2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_margin="10dp"
            android:src="@mipmap/v_ico2" />

        <TextView
            android:id="@+id/name2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_toRightOf="@id/ico2"
            android:text="淡淡的印象"
            android:textColor="#576B95" />

        <TextView
            android:id="@+id/content2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/name2"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:layout_toRightOf="@id/ico2"
            android:minLines="3"
            android:text="奋斗就是每一天都很难,可一年比一年容易。不奋斗就是每一天都很容易,可一年比一年难。怕吃苦的人吃苦一辈子,不怕吃苦的人吃苦一阵子。所以拼着一切代价奔你的前程;拼一个春夏秋冬,赢一个无悔人生!" />

        <TextView
            android:id="@+id/time2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/content2"
            android:layout_marginTop="3dp"
            android:layout_toRightOf="@id/ico2"
            android:text="6小时前"
            android:textColor="#9A9A9A" />

        <ImageView
            android:id="@+id/comment2"
            android:layout_width="30dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@id/content2"
            android:src="@mipmap/comment" />
    </RelativeLayout>

</LinearLayout>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值