1.安卓开发常用布局简述

一、常用布局

线性布局(Linear layout

相对布局(Relative layout

帧布局(Frame layout)

表格布局(Table layout)

网格布局(Grid layout)

约束布局(Constrained layout)

二、添加布局

1.利用XML文件设计

在这里插入图片描述
把写好的xml文件用R.layout引用出来

2.利用java代码添加

在这里插入图片描述
因为setContentView方法还有可以调用视图(view)的
所以:

        //1.根布局为线性布局
        LinearLayout fff=new LinearLayout(this);

        //2.设置宽高,match铺满,第一个参数代表宽度,第二个参数代表高度
        fff.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
        ViewGroup.LayoutParams.MATCH_PARENT));

        //3.背景设置为红色
        fff.setBackgroundColor(Color.RED);

        //4.指定此Activity的内容视图(view)为该线性布局

        setContentView(fff);

在这里插入图片描述
如果要继续+控件,使用在这里插入图片描述

三、布局重要属性

1. android:layout_width=" " 宽度

  • match_parent 匹配|适配父元素窗口大小
  • wrap_content 实际大小随着内容的变化而改变
  • 实际的数值 60dp/80sp
android:layout_width="wrap_content"
android:layout_width="match_parent"

2. android:layout_height=" " 高度

android:layout_height="wrap_content"
android:layout_height="match_parent"

3. android:padding=" " 内边距

android:padding="20dp"

4.android:layout_margin=" " 外边距

 android:layout_margin="20dp"

四、线性布局(Linear layout

4.0线性布局重要属性

1. android:orientation=" " 方向
<!--    vertical:垂直的  horizontal:水平的-->

 android:orientation="vertical"
 android:orientation="horizontal"

在这里插入图片描述

<?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"
    tools:context=".MainActivity"
    android:orientation="vertical"
    
    android:layout_weight=" "
    android:layout_gravity="center_horizontal">
<!--    vertical:垂直的  horizontal:水平的-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:layout_margin="20dp"
        android:padding="20dp"
        android:background="#ED380B" />  
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:layout_margin="20dp"
        android:padding="20dp"
        android:background="#18FDE3" />    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:layout_margin="20dp"
        android:padding="20dp"
        android:background="#84F852" />    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:layout_margin="20dp"
        android:padding="20dp"
        android:background="#F8EF52" />   
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:layout_margin="20dp"
        android:padding="20dp"
        android:background="#B229FD" />
  

</LinearLayout>

在这里插入图片描述

<?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"
    tools:context=".MainActivity"
    android:orientation="horizontal"

    >
<!--    vertical:垂直的  horizontal:水平的-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
       android:textSize="15sp"
        android:background="#ED380B"/>   
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"

    android:textSize="15sp"
        android:background="#2197F1" />   
  <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
    android:textSize="15sp"
        android:background="#23F186" />    
 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
    android:textSize="15sp"
        android:background="#951BFF" />    
 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
    android:textSize="15sp"
        android:background="#FBE346" />


</LinearLayout>
2.android:layout_weight=" "权重
     android:layout_weight="1"

放在控件中,
显示图示效果
在这里插入图片描述
在这里插入图片描述

<?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"
    tools:context=".MainActivity"
    android:orientation="horizontal"

    >
    <!--    vertical:垂直的  horizontal:水平的-->
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="你好"
        android:layout_weight="1"
        android:textSize="20sp"
        android:background="#ED380B"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="你好"
        android:layout_weight="2"
        android:textSize="20sp"
        android:background="#2197F1" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="你好"
        android:layout_weight="1"
        android:textSize="20sp"
        android:background="#23F186" />



</LinearLayout>

特别注意

水平布局时,宽度设置为0dp
垂直布局时,高度设置为0dp

否则
在这里插入图片描述
相反,改成0dp的话,就只会固定比例
在这里插入图片描述

3.android:layout_gravity=" " 重力

在这里插入图片描述
在这里插入图片描述

【另】android:gravity=" "是控制控件里的内容

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rock Hang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值