Android studio之UI组件
布局管理器
布局可以嵌套,布局里面可以写一堆布局和View
1.线性布局(LinearLayout)
默认靠左靠上
最常用属性:
android:id(标识) android:layout_margin(外边距)
android:layout_width(宽度) android:layout_padding(内边距)
android:layout_height(高度) android:orientation(方向)
android:background(背景)
用法
android:layout_width="match_parent" //匹配父控件的宽度 android:layout_width="wrap_content" //包含内容的宽度,就是内容有多少就有多宽 android:orientation="vertical" //垂直方向布局 android:orientation="horizontal"// android:background="#000000" //后面跟颜色的代码 android:id="@+id/accessibility_custom_action_11"//资源名字 android:layout_width="200dp"//自己写宽度或高度——高度宽度单位一般用dp,字体用sp View是所有控件的父类,首字母要大写,不然会变灰色 android:padding="20dp"//表上下左右都空20dp android:paddingLeft="20dp"//左边 android:paddingBottom="20dp"//下边 android:paddingRight="20dp">//右边 android:paddingTop="20dp"//上 android:layout_marginTop="20dp">//与上一个控件的距离 android:layout_marginBottom="20dp"//与下一个控件的距离 android:layout_marginLeft="15dp"//左边距 android:layout_marginRight="15dp"//右边距 android:gravity="bottom"//内部元素在左下角 android:gravity="center" //内部元素在中间 android:gravity="right"//内部元素在 android:gravity="center_vertical"//垂直居中 android:gravity="center_horizontal"//水平居中 android:layout_weight="1"//全中,里面写数字,多个时,按分数平分,如果子控件有占dp的话要先减去,然后把剩下的平分(一般设为0dp) <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" **droid:orientation="vertical"** tools:context=".MainActivity"> <!--开始是默认水平排列的,android:orientation="vertical"这个是后面才加的--> <LinearLayout android:id="@+id/accessibility_custom_action_11" android:layout_width="200dp" android:layout_height="200dp" android:background="#000000" android:orientation="vertical" android:padding="20dp" android:layout_marginBottom="20dp"> <View android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FF0033" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="200dp" android:background="#0066FF" android:orientation="horizontal" android:layout_marginTop="20dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" > <View android:layout_width="0dp" android:layout_height="match_parent" android:background="#000000" android:layout_weight="1"/> <!-- 默认是从左从上开始排列,所以在左上角--> <View android:layout_width="0dp" android:layout_height="match_parent" android:background="#FF0033" android:layout_weight="1"/> <View android:layout_width="0dp" android:layout_height="match_parent" android:background="#55AA99" android:layout_weight="1"/> </LinearLayout> </LinearLayout>
2.相对布局(RelativeLayout)
默认都是靠左靠上
-
最常用属性
android:layout_toLeftOf(在谁的左边) android:layout_below(在谁的下边)
android:layout_toRightOf(在谁的右边)
android:layout_alignBottom(跟谁的底部对齐)android:layout_alignParentBottom(跟父控件底部对齐)
用法
android:layout_alignParentBottom="true"//靠父控件底部对齐,true就是执行 android:layout_alignParentRight="true"//父控件右边 android:layout_toRightOf="@id/view_1"//表在@id/view_1的右边 android:layout_below="@id/view_1"//表在@id/view_1的下边
套娃娃
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <View android:id="@+id/view_1" android:layout_width="100dp" android:layout_height="100dp" android:background="#000000" /> <View android:id="@+id/view_2" android:layout_width="100dp" android:layout_height="100dp" android:background="#FF0033" android:layout_below="@id/view_1"/> <LinearLayout android:id="@+id/view_3" android:layout_width="match_parent" android:layout_height="200" android:layout_below="@id/view_2" android:background="#0066FF" android:orientation="horizontal" android:padding="15dp"> <View android:layout_width="100" android:layout_height="match_parent" android:background="#FF0033"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000" android:padding="15dp"> <View android:id="@+id/view_4" android:layout_width="100dp" android:layout_height="match_parent" android:background="#FF9900" /> <View android:id="@+id/view_5" android:layout_width="100dp" android:layout_height="match_parent" android:background="#ffffff" android:layout_toRightOf="@id/view_4" android:layout_marginLeft="10dp"/> </RelativeLayout> </LinearLayout> </RelativeLayout>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?