Android (4) 布局

布局也属于一种"控件",所有控件的父类就是View控件

1 线性布局

        各个子组件之间呈线性(垂直/水平)排列

1.1 常用属性

android:id布局id
android:layout_width宽度wrap_content匹配内容  match_parent匹配父控件
android:layout_height高度
android:background背景
android:layout_margin外边距
android:padding内边距也可以单独设置某一方向 paddingLeft  paddingRight.....
android:gravity内部子元素排布方式top上  bottom下  left左  right右   center居中  center_vertical垂直居中
android:weight权重2个子控件各占1就表示平分父控件剩余位置(通常将宽度/高度设置为0),一个占1一个占2就表示前者1/3后者2/3
android:orientation线性方向(线性布局特有)vertical(默认)   horizon

1.2 代码示例

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:paddingTop="10dp"
        android:paddingBottom="20dp"
        android:paddingLeft="40dp"
        android:paddingRight="90dp"
        android:background="#00FF00">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FF0000"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginTop="10dp"
        android:gravity="center_vertical"
        android:background="#00FFFF">
        <LinearLayout
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="#000000"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginTop="10dp"
        android:gravity="center_vertical"
        android:background="#4098d6">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#ff9900"
            tools:ignore="Suspicious0dp" />
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="#956fe7"
            tools:ignore="Suspicious0dp" />
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#ed7976"
            tools:ignore="Suspicious0dp" />
    </LinearLayout>

</LinearLayout>

2 相对布局

        各个子组件中,一个组件相对于另一个组件的位置

2.1 常用属性

android:layout_toLeftOf在who左边
android:layout_toRightOf在who右边
android:layout_below在who下面
android:layout_alignBottom与who底部对齐
android:layout_alignParentBottom与父控件底部对齐  (还有alignParentRight等等.....)

2.2 代码示例

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="xyz.aboluo.MainActivity">

    <View
        android:id="@+id/view_1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#000000"
        android:layout_alignParentRight="true"/>

    <View
        android:id="@+id/view_2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#FF0000"
        android:layout_below="@id/view_1"/>
    
</RelativeLayout>

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值