安卓开发入门教程-常用布局

tech2022-09-13  104

关注 安卓007 ,免费获取全套安卓开发学习资料

什么是FrameLayout

FrameLayout又称帧布局,开发中很少使用,因其定位方式过于简单,所有控件都默认定位左上角.也支持将子控件显示在父控件的上下左右及正中间.

基础样例

1. 默认定位样例

效果图

代码

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是文本显示控件TextView" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是按钮" /> </FrameLayout>

2. 相对父控件定位

效果图

代码

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="start" android:text="左上" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end" android:text="右上" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="中心按钮" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:text="左下" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:text="右下" /> </FrameLayout>

代码说明:

android:layout_centerInParent,设置是否在父控件中居中(横向和纵向).android:layout_alignParentStart,设置是否和父控件左对齐.android:layout_alignParentEnd,设置是否和父控件右对齐.android:layout_alignParentTop,设置是否和父控件上对齐(因为默认就是上对齐的,所以就不用设置了).android:layout_alignParentBottom,设置是否和父控件下对齐.

基础样例完整源代码

https://gitee.com/cxyzy1/FrameLayoutDemo

常用属性说明

属性名用途android:layout_width设置控件宽度,可设置为:match_parent(和父控件一样),wrap_content(按照内容自动伸缩),设置固定值(如200dp)android:layout_height设置控件高度,可设置为:match_parent(和父控件一样),wrap_content(按照内容自动伸缩),设置固定值(如200dp)android:background设置背景,可以是色值(如#FF0000)或图片等android:visibility可选值: visible(显示), invisible(隐藏,但是仍占据UI空间),gone(隐藏,且不占UI空间)android:layout_gravity当前控件在父控件中的位置,可选值:start(居左),end(居右),center(居中),top(居上),bottom(居下),属性可组合,用竖线分隔.

更多属性及实际效果,可以在开发工具里自行体验.


安卓开发入门教程系列汇总

开发语言学习

Kotlin语言基础

UI控件学习系列

UI控件_TextView UI控件_EditText UI控件_Button UI控件_ImageView UI控件_RadioButton UI控件_CheckBox UI控件_ProgressBar

关注头条号,第一时间获取最新文章:

最新回复(0)