Android布局

本文详细介绍了Android中的基础布局,包括LinearLayout的水平和垂直排布,权重分配,以及RelativeLayout的基本框架和对齐方式。同时,提到了常用控件如TextView、EditText、ImageView和Button的使用,并强调了资源文件在布局设计中的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1. Android的基础布局

2. LinearLayout 线性布局

3. RelativeLayout

4. 常用的控件


1. Android的基础布局

LinearLayout 线性布局

RelativeLayout 相对布局

TableLayout 表格布局

FrameLayout 帧布局(框架布局)

ConstrantLayout 约束布局 (Android Studio默认布局) 用于拖拽的

2. LinearLayout 线性布局

2.1 怎么将Android Studio默认的ConstrantLayout改为LinearLayout

1. 在design页面下->component tree->ConstrainLayout右键->Convert view...->选择LinearLayout 点击apply
1. 在code页面下->直接修改代码 将 androidx.constraintlayout.widget.ConstraintLayout  改为 LinearLayout

2.2 线性布局有两种:

  1. 水平的线性布局 所有控件都是水平挨个排布

    如果没有android:orientation属性的存在

    或者

    android:orientation="horizontal"
  2. 垂直的线性布局 所有控件都是垂直挨个排布

    android:orientation="vertical"

tips: 在android中,所有在页面上显示的东西,必须具备两个属性,这两个属性是宽和高

android:layout_width 宽度

Android:layout_height 高度

对于宽度和高度,他们的值有三个

1. wrap_content  按照内容自适应
1. match_parent  按照父容器尺寸填满
1. 50dp                  数值(用的地方很单一)               

2.3 比重:

android:layout_weight 
​
如何算总比重:  看同一父亲且同一级的各个控件的weight
一旦weight属性生效,android:layout_width失效

2.4 布局排布:

android:gravity                  内容位置改变
​
android:layout_gravity           本身位置改变  

2.5 分隔线:内部的线

android:divider="@color/black"
android:showDividers="middle"

2.6 嵌套线性布局结构

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
​
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
    </LinearLayout>
​
</LinearLayout>

2.6属性

属性说明
android:id唯一值
android:layout_height高,wrap_content:(随内容变化,类似auto),match_parent:(同父元素一样)单元最好是:dp
android:layout_width宽,同上
android:background背景色
android:layout_margin外边距
android:layout_padding内边距
android:orientationhorizontal水平排列(默认);vertical竖直排列
android:layout_weight权重
android:gravity排布方式

3. RelativeLayout

3.1基本框架:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    xmlns:tools="http://schemas.android.com/tools"    
    android:id="@+id/RelativeLayout1"    
    android:layout_width="match_parent"    
    android:layout_height="match_parent" >    
​
<ImageView    
    android:id="@+id/img1"     
    android:layout_width="80dp"    
    android:layout_height="80dp"    
    android:layout_centerInParent="true"    
    android:src="@drawable/pic1"/>    
​
...
</RelativeLayout>

3.2属性:

  • 相对于兄弟元素

属性名称属性含义
android:layout_below="@id/aaa"在指定View的下方
android:layout_above="@id/aaa"在指定View的上方
android:layout_toLeftOf="@id/aaa"在指定View的左边
android:layout_toRightOf="@id/aaa"在指定View的右边
android:layout_alignTop="@id/aaa"与指定View的上边界一致
android:layout_alignBottom="@id/aaa"与指定View下边界一致
android:layout_alignLeft="@id/aaa"与指定View的左边界一致
android:layout_alignRight="@id/aaa"与指定View的右边界一致
  • 相对于父元素

属性名称属性含义
android:layout_alignParentLeft="true"在父元素内左边
android:layout_alignParentRight="true"在父元素内右边
android:layout_alignParentTop="true"在父元素内顶部
android:layout_alignParentBottom="true"在父元素内底部
  • 对齐方式

属性名称属性含义
android:layout_centerInParent="true"居中布局
android:layout_centerVertical="true"垂直居中布局
android:layout_centerHorizontal="true"水平居中布局
  • 间隔

属性名称属性含义
android:layout_marginBottom=""离某元素底边缘的距离
android:layout_marginLeft=""离某元素左边缘的距离
android:layout_marginRight =""离某元素右边缘的距离
android:layout_marginTop=""离某元素上边缘的距离
android:layout_paddingBottom=""往内部元素底边缘填充距离
android:layout_paddingLeft=""往内部元素左边缘填充距离
android:layout_paddingRight =""往内部元素右边缘填充距离
android:layout_paddingTop=""往内部元素右边缘填充距离

###

4. 常用的控件

TextView 文本控件 给用户一个文字性的提示

EditText 输入文本控件

ImageView 图片控件 显示图片

Button 按钮

TextView

  1. 掌握常用的属性

  2. 掌握资源文件的使用

    如何创建资源文件,并对资源文件编程
    1.  

    2.  store:设置边框

      corners:设置边框弧形半径

      gradient:设置渐变色

      solid:设置填充色

      padding:设置内边距

       
    3. 使用资源文件

      android:background="@drawable/设置的名字"
  3. 跑马灯 (自己探索)

  4. 带图片的TextView

    android:drawableTop :在文字上边
    android:drawableBottom :在文字下边
    android:drawableLeft : 在文字左边
    android:drawableRight: 在文字右边

重点1:怎么样设置一个shaper(皮肤)

在drawable文件夹下设置 xml

重点2:带图片的TextView

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值