布局(LinearLayout,RelativeLayout,FrameLayout,TableLayout,GridLayout,ConstraintLayout)
LinearLayout
- layout_gravity:组件在父容器里的对齐方式
- gravity:组件包含的所有子元素的对齐方式
- layout_weight:在原有基础上分配剩余空间,一般把layout_height都设置为0dp再使用此属性
- 设置分割线可以用divider属性,或者插入View
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal|bottom"
android:divider="@drawable/ic_baseline_horizontal_rule_24"
android:showDividers="middle"
android:dividerPadding="20dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<View android:layout_height="1dp"
android:layout_width="match_parent"
android:background="#ff0000"/>
<LinearLayout android:layout_height="100dp"
android:layout_gravity="left"
android:layout_width="100dp"
android:background="#ff0000"/>
<LinearLayout android:layout_height="100dp"
android:layout_width="100dp"
android:background="#00ff00"/>
<LinearLayout android:layout_height="100dp"
android:layout_width="100dp"
android:background="#0000ff"/>
</LinearLayout>
RelativeLayout
- 可以根据父容器定位,也可以根据兄弟组件定位
- margin设置组件与父容器的边距
- padding设置组件内部元素的边距
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:padding="10dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:id="@+id/lt1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:background="#ff0000"/>
<RelativeLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_above="@id/lt1"
android:layout_margin="100dp"
android:background="#00ff00"/>
</RelativeLayout>
FrameLayout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<FrameLayout android:layout_height="300dp"
android:layout_width="300dp"
android:background="#ff0000"/>
<FrameLayout android:layout_height="200dp"
android:layout_width="200dp"
android:foreground="@drawable/ic_baseline_language_24"
android:foregroundGravity="right|bottom"
android:background="#0000ff"/>
<FrameLayout android:layout_height="100dp"
android:layout_width="100dp"
android:background="#00ff00"/>
</FrameLayout>
TableLayout
collapseColumns:隐藏列,从0开始
stretchColumns:有剩余空间才会拉伸
android:collapseColumns="1,3"
android:stretchColumns="2"
- shrinkColumns:有挤压的时候才能收缩
- layout_column:显示在第几列
- layout_span:横向跨几列
<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:shrinkColumns="2"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_span="2"
android:text="按钮0"/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_column="3"
android:text="按钮1"/>
</TableRow>
<TableRow>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="按钮0"/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="按钮1"/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="按钮2"/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="按钮3"/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="按钮4"/>
</TableRow>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="按钮0"/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="按钮1"/>
</TableLayout>
GridLayout
- columnCount、rowCount:最大行列数,与orientation配合使用
- layout_row、layout_column:显示所在行列
- layout_columnWeight:横向剩余空间分配方式
- layout_columnSpan:横向跨列,配合layout_gravity使用
<?xml version="1.0" encoding="utf-8"?>
<GridLayout android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="按钮0"/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_row="1"
android:layout_column="0"
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:text="按钮1"/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_row="0"
android:layout_column="1"
android:text="按钮2"/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="按钮3"/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="按钮4"/>
</GridLayout>
ConstraintLayout
......
自定义布局
- 创建一个标题布局title.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="#FFFF00"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/btn_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回"/>
<TextView
android:text="标题"
android:layout_gravity="center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"/>
<Button
android:id="@+id/btn_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="菜单"/>
</LinearLayout>
- 在onCreate()中关闭自带的标题栏
// 隐藏标题栏,使用自定义的
ActionBar actionBar = getSupportActionBar();
if (actionBar != null){
actionBar.hide();
}
- 在activity_main.xml中引用布局文件
<include layout="@layout/title"/>
自定义控件
- 也可以用控件的方式使用,TitleLayout.java
package com.example.myalterdialog;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.annotation.Nullable;
public class TitleLayout extends LinearLayout {
public TitleLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context) // 构建出一个LayoutInflater对象
.inflate(R.layout.title, this); // 第二个参数是给加载好的布局添加一个父布局
findViewById(R.id.btn_left).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "返回", Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.btn_right).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "菜单", Toast.LENGTH_SHORT).show();
}
});
}
}
- activity_main.xml中使用自定义控件
<com.example.myalterdialog.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
布局(LinearLayout,RelativeLayout,FrameLayout,TableLayout,GridLayout,ConstraintLayout)的更多相关文章
- Android四种基本布局(LinearLayout \ RelativeLayout \ FrameLayout \ TableLayout)
------------------------------------------LinearLayout---------------------------------------------- ...
- android 基本布局(RelativeLayout、TableLayout等)使用方法及各种属性
本文介绍 Android 界面开发中最基本的四种布局LinearLayout.RelativeLayout.FrameLayout.TableLayout 的使用方法及这四种布局中常用的属性. ...
- 浅谈Android五大布局——LinearLayout、FrameLayout、AbsoulteLayout、RelativeLayout和TableLayout
Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建 筑里的砖瓦.组件按照布局的要求依次排列,就组成了用户所看见的界面.Android的五大布局分别是LinearLa ...
- Android精通:TableLayout布局,GridLayout网格布局,FrameLayout帧布局,AbsoluteLayout绝对布局,RelativeLayout相对布局
在Android中提供了几个常用布局: LinearLayout线性布局 RelativeLayout相对布局 FrameLayout帧布局 AbsoluteLayout绝对布局 TableLayou ...
- Android UI -- 布局介绍(布局包括FrameLayout, LinearLayout, RelativeLayout, GridLayout)
首先介绍常用布局类 FrameLayout 最简单的布局管理器. 这个布局管理类有几个特性: 添加组件默认在左上角的. 如果添加多个组件会叠加到一起,并且都在左上角.(可以通过一gravity属性改变 ...
- Android 五大布局(LinearLayout、FrameLayout、AbsoulteLayout、RelativeLayout、TableLayout )
前言 欢迎大家我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net ...
- android 学习 之 布局(下)LinearLayout,RelativeLayout,TableLayout,FrameLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...
- 如何优化你的布局层级结构之RelativeLayout和LinearLayout及FrameLayout性能分析
转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/51159419 如何优化你的布局层级结构之RelativeLayout和LinearLa ...
- Android精通:View与ViewGroup,LinearLayout线性布局,RelativeLayout相对布局,ListView列表组件
UI的描述 对于Android应用程序中,所有用户界面元素都是由View和ViewGroup对象构建的.View是绘制在屏幕上能与用户进行交互的一个对象.而对于ViewGroup来说,则是一个用于存放 ...
- [转]浅谈Android五大布局(二)——RelativeLayout和TableLayout
在浅谈Android五大布局(一)中已经描述了LinearLayout(线性布局).FrameLayout(单帧布局)和AbsoulteLayout(绝对布局)三种布局结构,剩下的两种布局Relati ...
随机推荐
- AI大模型 —— 国产大模型 —— 华为大模型
有这么一句话,那就是AI大模型分两种,一种是大模型:另一种是华为大模型. 如果从技术角度来分析,华为的技术不论是在软件还是硬件都比国外的大公司差距极大,甚至有些技术评论者认为华为的软硬件技术至少落后2 ...
- python库 —— gym retro 的 ROMs文件下载地址
如题: python库 -- gym retro 的 ROMs文件下载地址: https://archive.org/details/No-Intro-Collection_2016-01-03_Fi ...
- git clone 如何通过proxy进行远程代码仓库拷贝下载
git使用proxy的方式和ssh的情况是差不多的,给出借鉴: SSH如何通过proxy进行服务器连接 ------------------------------------------------ ...
- Long Way to be Non-decreasing 题解
前言 题目链接:洛谷:CF. 题意简述 yzh 喜欢单调不降序列. 她有一个序列 \(a\),最初为 \(a_1, \ldots, a_n\),其中每个元素都在 \([1, m]\) 内. 她希望使序 ...
- 这应该是全网最全的CSP-S初赛复习吧
点我到洛谷看 \(Update\ 2024/8/2:\) 加入了在数据结构中增加了"树",做出部分更改. linux基础命令 cd 切换目录 ls 列出目前工作目录所含的文件及子目 ...
- 白鲸开源CEO郭炜荣获「2024中国数智化转型升级先锋人物」称号
2024年7月24日,由数据猿主办,IDC协办,新华社中国经济信息社.上海大数据联盟.上海市数商协会.上海超级计算中心作为支持单位,举办"数智新质·力拓未来 2024企业数智化转型升级发展论 ...
- vite创建的react项目如何兼容低版本安卓,低版本安卓不支持es6语法
Vite 是一个现代化的前端构建工具,默认情况下,它会生成基于 ES6+ 的代码.这对于大多数现代浏览器来说是没有问题的,但对于一些较旧版本的安卓浏览器可能会遇到兼容性问题. 为了使 Vite 创建的 ...
- .NET+WPF 桌面快速启动工具 GeekDesk
前言 大家在平时工作中,是不是经常为了找某个文件或者应用而在电脑桌面上来回翻找?桌面图标乱七八糟,每次找东西都像在大海捞针一样. 今天给大家介绍一个开源项目 GeekDesk,它能够让桌面焕然一新,工 ...
- uniapp中,getApp()返回的实例到底是什么?为什么getApp()返回的实例无法访问vuex的$store
按uniapp官方手册中说,getApp()函数用于获取当前应用实例.当前应用,也就是说当前应用程序.因为getApp()返回的实例可以用于访问app.vue中的globaldata,因此这个当前应用 ...
- 入门指南 | Datavines 安装部署篇
摘要:本文主要介绍基于源码部署 Datavines 和执行检查作业,内容主要分为以下几个部分: 平台介绍 快速部署 运行数据质量检查作业 Datavines 的目标是成为更好的数据可观测性领域的开源项 ...