Android布局初学习
布局指对界面结构的全面规划与安排,通过api中提供的各种布局能够快速的完成对于界面的设计
常用布局
线性布局(LinearLayout)
相对布局(RelativeLayout)
帧布局(FrameLayout)层层叠加
表格布局(TableLayout)
网格布局(GridLayout)
约束布局(ConstraintLayout)
添加布局
Java方法
setContentView()
例:
LinearLayout ll=new LinearLayout(this);//创建跟布局为线性布局的对象
setLayoutparams(new LinearLayout .Layoutparams(ViewGroup.Layoutparams.MATCH_PARENT,ViewGroup.Layoutparams.MATCH_PARENT))//设置宽高
ll.setBackgroundColor(Color.RED)//背景为红色
setContentView(ll)//制定此activity的内容视图为线性布局
xml方法
布局文件必须全小写可有数字但不能在开头也可有下划线
布局的重要属性
Android:layout_width宽度
Android:layout_height高度
match_parent匹配父容器
wrap_centent自适应内容大小
numberdp
Android:layout_padding内边距
Android:layout_margin外边距
linearlayout线性布局
线性布局重要属性(特有)
Android:orientation方向,horizontal水平(默认),vertical垂直
Android:layout_weight权重,控件在指定的方向上按照权重分配大小使用时将对应的宽高属性调为0dp
Android:layout_gravity重力偏向,偏向父容器那里bottom下,center中,top顶,left左,right右,center_horizontal水平居中,ceneter_vertical垂直居中,clip_horizontalclip_vertical
Android:gravity重力偏向内容偏向
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="#DCD7D7"android:orientation="horizontal"android:padding="5dp"><TextViewandroid:layout_width="36dp"android:layout_height="match_parent"android:layout_margin="5dp"android:text="<"android:textColor="#ffffff"android:textSize="40dp" /><TextViewandroid:layout_width="200dp"android:layout_height="match_parent"android:text="一号鱼"android:textSize="30dp"android:layout_margin="13dp"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:layout_gravity="bottom"android:background="#BFBEBE"android:layout_weight="3"android:orientation="horizontal"></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="70dp"android:background="#DCD7D7"><TextViewandroid:layout_width="40dp"android:layout_height="40dp"android:layout_gravity="bottom"android:layout_margin="3dp"android:text="/@"android:textSize="25dp" /><TextViewandroid:layout_width="match_parent"android:layout_height="40dp"android:layout_gravity="center_vertical"android:layout_margin="5dp"android:gravity="bottom"android:text="对方在等待海王的回复哦"android:textSize="25dp" /></LinearLayout></LinearLayout>
RelativeLayout相对布局
相对于父容器(取值:true/false)可搭配使用
android:layout_centerInParent="true"完全居中于父容器
ndroid:layout_alignParentLeft="true"左
ndroid:layout_alignParentRight="true"右
ndroid:layout_alignParentTop="true"顶
ndroid:layout_alignParentBottom="true"底部
android:layout_centerHorizontal="true"水平居中
android:layout_centerVertical="true"竖直居中
相对于其他控件(取值:其他控件的id)可搭配使用
android:id="@+id/idname"添加参照物id以便使用 参照物控件中添加
1在参照物的某边
android:layout_toLeftOf="@id/idname"在id的左边
android:layout_toRightOf="@id/idname"右边
android:layout_below="@id/idname"下面
android:layout_above="@id/idname"上面
2与参照物的边线对齐
android:layout_alignTop="@id/idname"上边线与上边线对齐
android:layout_alignBottom="@id/idname"下边线对齐
android:layout_alignLeft="@id/idname"左
android:layout_alignRight="@id/idname"右
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="2"android:background="#A87AF858"android:orientation="horizontal"><RelativeLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="2"android:background="#BFAD11"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="2D"android:textSize="50dp"></TextView><TextViewandroid:layout_width="wrap_content"android:layout_height="80dp"android:text="我上铺的兄弟"android:textSize="45dp"android:layout_alignParentBottom="true"android:background="#2053CD"></TextView></RelativeLayout><LinearLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="#95730E"android:orientation="vertical"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="2"android:background="#ff00ff"></RelativeLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="2"android:background="#ff00aa"></RelativeLayout></LinearLayout></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#F33576"><RelativeLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="#137ED3"></RelativeLayout><RelativeLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="#043359"></RelativeLayout><RelativeLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="#137ED3"></RelativeLayout></LinearLayout>
</LinearLayout>
帧布局(FrameLayout)层层叠加
android:foreground""前景
android:foregroundGravity前景重力
表格布局(TableLayout)
直接在TableLayout中添加控件,控件与父容器等宽如果想让控件在同一行,要在控件外层加一队<TableRow>中宽度默认为wrap_content
android:stretchColumns="0,1,2,3"可伸展的列也可用星*表示所有
android:shrinkColumns"0,2"可以收缩
android:collapseColumns隐藏列
网格布局(GridLayout)
android :rowCount行数量
android :columnCount列数量
android:layout_columnSpan="2"跨列占据两列但是并不会直接变化需要配合android:layout_gravity使用,或者配合宽
android:layout_rowSpan="2"跨行
约束布局(ConstraintLayout)
app:layout_constraintBottom_toBottomOf约束当前空间的底部位置left,right,top等
app:ayout_constraintHorizontal_bias水平偏移量0-1
app:ayout_constraintVertical_bias竖直偏移量
Android布局初学习相关推荐
- Android学习(七)—— Android布局
Android布局 1.LinearLayout 线性布局,这种布局在平时的开发中用的最多,内部控件只能水平或竖直进行排列,在搭建较复杂的界面时会有点麻烦. 常用属性 android:orientat ...
- [Android Studio] 初体验
[Android Studio] 初体验 本人刚开始接触移动开发方面的知识,在很多方面都感觉寸步难行,移动开发这门课程应该是在我一年后学校才会开设,而移动开发所用到的java也是在我下个学期才开始正式 ...
- 使用Kotlin开发Android应用初体验
使用Kotlin开发Android应用初体验 昨晚,最近一届的谷歌IO大会正式将Kotlin确定为了官方开发语言,作为一名Android开发鸟,怎么能不及时尝尝鲜呢? Kotlin的简要介绍 在开发之 ...
- android 布局之RelativeLayout(相对布局)
android 布局分为LinearLayout TableLayout RelativeLayout FreamLayout AbsoluteLayout. 常用的有LinearLayout,Tab ...
- android表格布局最后一个组件,Android布局之TableLayout表格布局
Tablelayout类以行和列的形式对控件进行管理,每一行为一个TableRow对象,或一个View控件.当为TableRow对象时,可在TableRow下添加子控件,默认情况下,每个子控件占据一列 ...
- 浅谈Android布局
在前面的博客中,小编介绍了Android的极光推送以及如何实现登录的一个小demo,对于xml布局页面,摆控件这块的内容,小编还不是很熟练,今天小编主要简单总结一下在Android中的布局,学习过An ...
- Android布局管理器-使用LinearLayout实现简单的登录窗口布局
场景 Android布局管理器-从实例入手学习相对布局管理器的使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1038389 ...
- Android布局加载慢,Android布局优化(四)X2C — 提升布局加载速度200%
系列文章 前言 在Android布局优化(一)从布局加载原理说起中我们说到了布局加载的两大性能瓶颈,通过IO操作将XML加载到内存中并进行解析和通过反射创建View.这里介绍一种避免运行时通过IO操作 ...
- android 自定义圆形pop,Android布局自定义Shap圆形ImageView可以单独设置背景与图片...
一.图片预览: 一.实现功能: 需求要实现布局中为圆形图片,图片背景与图标分开且合并到一个ImageView. 二.具体实现: XML中布局中定义ImageView,关健设置两个参数 Android: ...
最新文章
- ibatis.net私人学习资料,勿下(加密)
- 零基础Python培训入门 教你认识下这些基础内容
- 【Elasticsearch】 es watcher 视频 笔记
- 非root用户安装mysql_linux非root用户安装5.7.27版本mysql
- 【转载】关于.NET下开源及商业图像处理(PSD)组件
- UDP socket 编程示例
- unionid openid微信php,微信的openID、unionID等总结
- matlab 模型运行速度,用matlab求解超效率DEA模型运行结果的辨认
- cad计算机制图如何标注,零件序号和图号有什么区别,CAD制图中怎样标注零件序号...
- yoga710怎么进入bios_联想_ThinkPad|ThinkCentre|ThinkStation服务与驱动下载_常见问题
- WebView 加载微信公众号文章加载失败或图片不显示
- 原生js的animate方法
- [NSSCTF][SCTF 2021]WEB复现
- java设计游戏_用java设计实现一个简单的动作冒险类游戏
- Function是什么?
- Paper:机器学习、深度学习常用的国内/国外引用(References)论文参考文献集合(建议收藏,持续更新)
- Drupal theme-2 安装主题
- RK3588使用OV5695摄像头
- Practical C++ Programming电子书pdf下载
- linux表示隐藏文件的格式,文件查看 -- ls