安卓基本布局(上)

文章目录

LinerLayout线性布局

以水平或垂直的方式来排列界面中的控件。

常用属性 详细描述
orientation 布局中组件的排列方式。horizonta:水平;vertical:竖直(默认)。
gravity 控制组件所包含的子元素的对齐方式,可以多个组合。
layout_gravity 控制该组件在父容器里的对齐方式
layout_width 布局宽度,wrap_content:组件实际大小;fill_parent、match_parent:填满父容器;固定值。
layout_height 布局高度,wrap_content:组件实际大小;fill_parent、match_parent:填满父容器;固定值。
id 为该组件设置一个资源id,在Java文件中可以通过findViewById找到该组件
background 为该组件设置一个背景图片或直接用颜色覆盖
xml 复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:background="#ADFF2F"
        android:layout_weight="3"
        android:gravity="bottom|left">
        <Button
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="one"
            />
        <Button
            android:layout_weight="3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="two"
            />
    </LinearLayout>


    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:background="#DA70D6"
        android:layout_weight="1"/>

</LinearLayout>

RelativeLayout相对布局

通过相对定位的方式让控件出现在布局的任何位置。

基本属性 详细描述
gravity 设置容器内组件的对齐方式。
ignoreGravity 该属性为true的组件,将不受gravity属性影响。
根据父容器定位
属性 描述
layout_alignParentLeft 左对齐
layout_alignParentRight 右对齐
layout_alignParentTop 顶部对齐
layout_alignParentBottom 底部对齐
layout_centerHorizontal 水平居中
layout_centerVertical 垂直居中
layout_centerInParent 中间居中
xml 复制代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
	<!-- 1会被2覆盖-->
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="1" />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="2" />
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="3" />
    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="4" />
    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="5" />
    <!-- 6会被7覆盖-->
    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:text="6" />
    <Button
        android:id="@+id/button7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="7" />
    <Button
        android:id="@+id/button8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:text="8" />
    <Button
        android:id="@+id/button9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="9" />
</RelativeLayout>
根据兄弟组件定位
属性 描述
layout_toLeftOf 参考组件的左边
layout_toRightOf 参考组件的右边
layout_above 参考组件的上方
layout_belove 参考组件的下方
layout_alignTop 对齐参考组件的上边界
layout_alignBottom 对齐参考组件的下边界
layout_alignLeft 对齐参考组件的左边界
layout_alignRight 对齐参考组件的右边界
xml 复制代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/textview"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#13d169"
        android:layout_centerInParent="true" />
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/textview"
        android:text="1" />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/textview"
        android:text="2" />
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textview"
        android:text="3" />
    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textview"
        android:text="4" />
    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textview"
        android:text="5" />
    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textview"
        android:text="6" />
    <Button
        android:id="@+id/button7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textview"
        android:text="7" />
    <Button
        android:id="@+id/button8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textview"
        android:text="8" />
</RelativeLayout>
margin偏移
属性 描述
layout_margin 设置组件上下左右的偏移量
layout_marginLeft 设置组件离左边的偏移量
layout_marginRight 设置组件离右边的偏移量
layout_marginTop 设置组件离上面的偏移量
layout_marginBottom 设置组件离下面的偏移量
padding填充
属性 描述
padding 向内部元素的上下左右填充一定边距
paddingLeft 向内部元素的左边填充一定边距
paddingRight 向内部元素的右边填充一定边距
paddingTop 向内部元素的上方填充一定边距
paddingBottom 向内部元素的下方填充一定边距
相关推荐
历程里程碑3 分钟前
滑动窗口---- 无重复字符的最长子串
java·数据结构·c++·python·算法·leetcode·django
2603_949462107 分钟前
Flutter for OpenHarmony社团管理App实战:意见反馈实现
android·flutter
qq_2290580123 分钟前
docker中检测进程的内存使用量
java·docker·容器
一个天蝎座 白勺 程序猿29 分钟前
KingbaseES查询逻辑优化深度解析:从子查询到语义优化的全链路实践
开发语言·数据库·kingbasees·金仓数据库
错把套路当深情30 分钟前
android两种渠道支持一键打包 + 随意组合各种渠道
android
我真的是大笨蛋37 分钟前
InnoDB行级锁解析
java·数据库·sql·mysql·性能优化·数据库开发
钦拆大仁39 分钟前
Java设计模式-单例模式
java·单例模式·设计模式
云边散步1 小时前
godot2D游戏教程系列二(4)
笔记·学习·游戏开发
小手cool1 小时前
在保持数组中对应元素(包括负数和正数)各自组内顺序不变的情况下,交换数组中对应的负数和正数元素
java
笨手笨脚の1 小时前
深入理解 Java 虚拟机-04 垃圾收集器
java·jvm·垃圾收集器·垃圾回收