Androidstudio学习记录:android studio布局

本文介绍了Android中的五种主要布局类型:LinearLayout(线性布局),表格布局(TableRow和ImageView),相对布局(RelativeLayout),帧布局(FrameLayout)和绝对布局(AbsolLayout)。详细讲解了它们的用法、示例和特点。
摘要由CSDN通过智能技术生成

 

仅作记录以及参考,图片与文字无关awa

 

1、线性布局:

LinearLayout

控件从左到右排列:水平方式

       从上到下          垂直方式

标签:LinearLayout

 

   代码:<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"

 android:layout_width="match_parent"

 android:layout_height="match_parent"

    android:orientation="vertical">

    </LinearLayout> 

xmls:android;声明一个命名空间前缀,叫做android,http://schemas.android.com/apk/res/android,是网址格式,才可以使用android前缀。

orientation="vertical"是垂直排列

 

 代码:

 <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 android:layout_width="match_parent"

 android:layout_height="match_parent"

 android:orientation="vertical">

 <!-- 线性布局;Ctrl+Alt+L键:格式化代码;-->

 <!--

 1.行 用户名:输入框

 2.行 密码:输入框

 3.登录 取消

 -->

 <!-- 1.第一行线性布局:水平方式

 match_parent 表示使控件填充布局单元内的尽可能多的空间

 wrap_content:表示视图扩展以实现全部内容;

 layout_width:布局宽;

 layout_height:height:布局高度;

 txt:文本内容

 textSize:文本大小

 layout_weight:字体粗细

 paddingLeft:左填充

 paddingTop:上填充;

 -->

 <!-- 第一行,线性布局,水平方式-->

 <LinearLayout

 android:layout_width="match_parent"

 android:layout_height="wrap_content"

 android:orientation="horizontal">

 <TextView

 android:layout_width="166dp"

 android:layout_height="match_parent"

 android:layout_weight="1"

 android:paddingStart="10dp"

 android:paddingTop="10dp"

 android:text="用户"

 android:textSize="20dp" />

 <EditText

 android:layout_width="0dp"

 android:layout_height="wrap_content"

 android:layout_weight="3" />

 </LinearLayout>

 <!-- 第一行,线性布局,水平方式-->

 <LinearLayout

 android:layout_width="match_parent"

 android:layout_height="wrap_content">

 <TextView

 android:layout_width="0dp"

 android:layout_height="match_parent"

 android:layout_weight="1"

 android:paddingStart="10dp"

 android:paddingTop="10dp"

 android:text="密码"

 android:textSize="20dp" />

 <EditText

 android:layout_width="0dp"

 android:layout_height="wrap_content"

 android:layout_weight="3" />

 </LinearLayout>

 <!--第三行,线性布局,水平方式-->

 <LinearLayout

 android:layout_width="match_parent"

 android:layout_height="wrap_content">

 <Button

 android:layout_width="match_parent"

 android:layout_height="wrap_content"

 android:layout_weight="1"

 android:text="登录"/>

 <Button

 android:layout_width="match_parent"

 android:layout_height="wrap_content"

 android:layout_weight="1"

 android:text="取消"/>

 </LinearLayout>

</LinearLayout>

 

除了LineaLayout线性布局之外,其中有三个控件;

1.TextView:显示文本提示信息

2.EditText:输入框

3.Button:按钮

 

2、表格布局

TableRow ……

  ImageView……

是按照行和列来组织子视图的布局

包含一系列TableRow对象,用于定义行

TableLayout-->TableRow

    可以包含0个以上的单元格,每个单元格可以设置一个View视图。

从零开始的索引项,可以拉伸,*号大题拉伸的所有列。

    android:shrinkColumns="*" 

 

收缩从0开始的索引项

   android:stretchColumns="*" 

    <!-- 表格里面包含:TableRow -->

 <TableRow android:layout_weight="1">

<!-- 放置图片,ImageView ;src:表示图标路径:scaleType:自动适应大小 -->

 <ImageView

 android:layout_width="match_parent"

 android:layout_height="match_parent"

 android:src="@mipmap/ic_launcher"

 android:scaleType="fitXY"/>

 <ImageView

 android:layout_width="match_parent"

 android:layout_height="match_parent"

 android:src="@mipmap/ic_launcher_round"

 android:scaleType="fitXY"/>

 <ImageView

 android:layout_width="match_parent"

 android:layout_height="match_parent"

 android:src="@mipmap/ic_launcher"

 android:scaleType="fitXY"/>

 </TableRow>

 <TableRow android:layout_weight="1">

 <!-- 放置图片,ImageView ;src:表示图标路径:scaleType:自动适应大小 -->

 <ImageView

 android:layout_width="match_parent"

 android:layout_height="match_parent"

 android:src="@mipmap/ic_launcher"

 android:scaleType="fitXY"/>

 <ImageView

 android:layout_width="match_parent"

 android:layout_height="match_parent"

 android:src="@mipmap/ic_launcher_round"

android:scaleType="fitXY"/>

 <ImageView

 android:layout_width="match_parent"

 android:layout_height="match_parent"

 android:src="@mipmap/ic_launcher"

 android:scaleType="fitXY">

 </TableRow>

需要多少层放多少层

此时如果图片文件出错,将src后的图片名称全改为小写即可

 

3、相对布局

RelativeLayout

控件的位置相对于其他控件或者父容器而言。在进行设计的时候需要按照控件之间的依赖关系进行排列

增加一些控件

<!--增加几个按钮;

layout_centerInParent:如果为true,则表示该空间位于父控件中央位置;

layout_centerHorizontal:如果为true,则表示该空间位于父控件的居中位置;

layout_above;将该控件位于该控件的上方

layout_below:将该控件位于该控件的下方

layout_toLeftOf:将该控件的右边缘与给定Id的控件左边缘对齐;

layout_toRightOf:将该控件的右边缘与给定Id的控件左边缘对齐-->

   <RelativeLayout xmlns:tools="http://schemas.android.com/tools"

 xmls:android="http://schemas.android.com/apk/res/android"

 android:layout_width="match_parent"

 android:layout_height="match_parent"

 xmlns:android="http://schemas.android.com/apk/res/android"

 xmlns:xmls="http://schemas.android.com/apk/res-auto">

 <!--增加几个按钮;

 layout_centerInParent:如果为true,则表示该空间位于父控件中央位置;

 layout_centerHorizontal:如果为true,则表示该空间位于父控件的居中位置;

 layout_above;将该控件位于该控件的上方

 layout_below:将该控件位于该控件的下方

 layout_toLeftOf:将该控件的右边缘与给定Id的控件左边缘对齐;

 layout_toRightOf:将该控件的右边缘与给定Id的控件左边缘对齐-->

 <Button

 android:id="@+id/search_bar_text_view"

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:layout_centerInparent="true"

 android:text="中间" />

 <Button

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:id="@+id/bt_up"

 android:layout_above="@id/bt_up"

 android:layout_centerHorizontal="true"

 android:text="向上"/>

 <Button

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:id="@+id/bt_down"

 android:layout_below="@id/bt_center"

 android:layout_centerHorizontal="true"

 android:text="局下"

 tools:ignore="UnknownId" />

 <Button

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:id="@+id/bt_Left"

 android:layout_toLeftOf="@+id/bt_center"

 android:layout_centerHorizontal="true"

 android:text="居左" />

 <Button

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:id="@+id/bt_Right"

 android:layout_toRightOf="@+id/bt_center"

 android:layout_centerVertical="true"

 android:text="局下" />

</RelativeLayout> 

  

4、帧布局

FrameLayout

帧布局是布局中最简单的一个布局,整个界面被当做一块空白备用区域。所有的子元素都不能指定位置进行放置,全部放置于这块区域的左上角,并且后面的子元素直接覆盖前面的子元素,将前面的子元素部分部分或者覆盖

    整个屏幕作为一块区域,,在这个区域添加子控件。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent" >

<!--帧布局

android:layout_gravity :center 居中-->

<TextView

android:layout_width="400dp"

android:layout_height="440dp"

android:background="@color/white"

android:layout_gravity="center"/>

<TextView

android:layout_width="250dp"

android:layout_height="250dp"

android:background="@color/black"

android:layout_gravity="center"/>

 

5、绝对布局

绝对布局是所有控件的排列由开发哦人员通过控件来制定,容器不再负责管理子控件的位置。

AbsolLayout:子控件的位置和布局都需要通过坐标来指定,所以,在设计布局的时候,需要开发人员提供子元素精确的横坐标和纵坐标。

绝对布局由坐标控制,缺乏灵活性,维护起来不容易

layout_x:x坐标

手机上坐标原点和pc端一样在屏幕左上角。

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

<!-- 绝对布局 -->

    <TextView

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:text="我不是绝对布局"/>

    <Button

        android:layout_width="200dp"

        android:layout_height="wrap_content"

        android:layout_x="300px"

        android:layout_y="100px"

        android:text="我不打游戏"/>

</AbsoluteLayout>

 

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值