4种基本布局

一、线性布局LinearLayout

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:orientation="horizontal“
android:layout_width=“match_parent”
android:layout_height=“match_parent”>

<Btuuon
    android:id="@+id/button1"
    android:layout_width="0"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="button1"
    android:layout_gravity="top" />

1、**android:orientation**="vertical" 指定了排列方式,vertical为垂直方向上排列,horizontal为水平方向上排列。 注意点:排列方式为horizontal,内部控宽度不能指定为match_parent. 2、**android:layout_gravity**="top"制定控件在布局的对齐方式,android:layout_gravity的值与文字的gravity一样。 注意点:当布局采用horizontal排列,只有垂直方向的对齐方式才会有效。 3、EditText与Button的宽度都指定为0dp,使用**android:layout_weight**来指定控件的大小。dp是Android中用于指定文件大小、间距等属性的单位。 EditText与Button都是android:layout_weight="1",将在水平方向平分宽度。 4、若Button的宽度改为wrap_content,此时EiditText将会占满剩下屏幕所有的剩余空间。

二、相对布局 RelativeLayout

RelativeLayout称作相对布局。与LinearLayout的排列规则不同,RelativeLayout更加随意一些,可以通过相对定位的房是让控件出现在布局的任意位置。
《一》、相对于父布局的定位

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_aligrnParentTop="true"
        android:layout_alignParentLeft="true"
        android:text="Button1"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:text="Button2"/>
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Button3" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button4"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="Button4"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button5"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:text="Button5"/>
1、Button1和父布局的左上角对其,Button2和父布局的右上角对齐,Button3居中对齐,Button4和父布局的左下角对齐,Button5和父布局的右下角对齐。 **android:layout_alignParentRight android:layout_alignParentBottom android:layout_alignParentLeft android:layout_aligrnParentTop android:layout_centerInParent** **《二》、相对于控件进行定位**





1、 android:layout_above让控件位于另一控件上方,因为这个属性指定相对于控件ID的引用
android:layout_below让控件位于另一控件下方
android:layout_toLeftOf让控件位于另一控件左侧
android:layout_toRightOf让控件位于另一控件右侧
2、RelativeLayout另一种相对于控件进行的定位的属性。
android:layout_alignLeft让一个控件的左边缘和另一个控件的左边缘对齐
android:layout_alignRight让一个控件的右边缘和另一个控件的右边缘对齐
android:layout_alignTop让一个控件的上边缘和另一个控件的上边缘对齐
android:layout_alignBottom让一个控件的下边缘和另一个控件的下边缘对齐

三、帧布局 FrameLayout

这种布局没有方便的定位方式,所有的控件都会默认摆放在布局的左上角。

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"/>
1、TextView与ImageView控件都是位于布局的左上角,因为ImageView在TextView之后,在布局上显示图片在字体上面。可以使用layout_gravity属性来指定控件在布局中的对齐方式,与LinearLayout中的用法一样。

四、百分比布局(简单了解 一般不用 可看可不看)

注意:只有LinearLayout支持使用layout_weight属性来实现按比例指定控件大小的功能
1、百分比布局,允许直接指定控件在布局中所占的百分比,可以轻松实现平分布局和任意比例分割布局的效果。百分比布局只为RelativeLayout 、FrameLayout 进行了功能的扩展,提供了PercentRelativeLayout 和PercentFrameLayout这两个全新的布局。
2、百分比布局属于新增布局,Android将百分比布局定义在了support库当中,只需要在项目的building.gradle中添加百分比布局库的依赖,就能保证百分比布局在Android所有系统版本上的兼容性。

五、谷歌推荐的相对布局ConstraintLayout 与RelativeLayout相似

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
android:layout_width=“match_parent”
android:layout_height=“match_parent”>

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guide_1"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.25" />
    
<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guide_2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.5" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guide_3"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.75" />

<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toStartOf="@+id/guide_1"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toStartOf="@+id/guide_2"
    app:layout_constraintStart_toStartOf="@+id/guide_1"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toStartOf="@+id/guide_3"
    app:layout_constraintStart_toStartOf="@+id/guide_2"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/guide_3"
    app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值