android的布局文件简介

xiaoxiao2025-08-27  59

LinearLayout-线性布局:

LinearLayout-线性布局有两个方向:水平和垂直方向。分别是通过android:orientation="horizontal"和android:orientation="vertical"来控制的

权重,也就是对控件设置 android:layout_weight的属性。这个属性的意思是分配剩余空间

比如有俩个控件,分别设置为android:layout_weight=“1”,android:layout_weight=“2”,表示控件分别占屏幕的1/3和2/3,。不过这是有一个前提的,就是建立在控件的宽度或者高度设置为0dp的情况下。

例子:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity" > <!-- android:orientation="vertical" 一定要写到头文件中,不然会出错--> <!--第一个例子是垂直布局,第二个例子是横向布局--> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#120ff0" /> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#950ff0" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="#079ff0" /> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="#000fca" /> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="#000f65" /> </LinearLayout> </LinearLayout >

RelativeLayout-相对布局

相对,顾名思义是有参照的,就是以某个兄弟组件,或者父容器来决定的(兄弟组件是在一个同一个布局里面的组件,如果是布局里一个组件参照另一个布局里的组件会出错)

android:paddingBottom:设置控件内容与控件下边缘的距离

android:paddingTop:设置控件内容与控件上边缘的距离

android:paddingLeft:设置控件内容与控件左边缘的距离

android:paddingRight:设置控件内容与控件右边缘的距离

android:layout_marginBottom:设置此控件的下边缘与其他控件的距离

android:layout_marginTop:设置此控件的上边缘与其他控件的距离

android:layout_marginLeft:设置此控件的左边缘与其他控件的距离

android:layout_marginRight:设置此控件的右边缘与其他控件的距离

 

ConstraintLayout(约束布局)

ConstraintLayout 能够灵活地定位和调整子View的大小,子 View 依靠约束关系来确定位置。

功能:

相对定位 (横向:Left、Right、Start、End   ;纵向:Top、Bottom、Baseline(文本底部的基准线))外边距居中和倾向可见性的表现尺寸约束Chain辅助工具

一些基本属性:

 

 

转载请注明原文地址: https://www.6miu.com/read-5035288.html

最新回复(0)