Android下有五大布局,分别是:

LinearLayout 线性布局

RelativeLayout 相对布局

AbsoluteLayout 绝对布局

TableLayout 表格布局

FrameLayout 帧布局

布局是一种可用于放置很多控件的容器,它可以按照一定的规律调整内部控件的位置,从而编写出精美的界面。

1.LinearLayout

LinearLayout又称作线性布局,是一种非常常用的布局。通过android:orientation属性指定排列方向,垂直方向是vertical,水平方向是horizontal。

<?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" >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="按钮1" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="按钮2" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="按钮3" /></LinearLayout>

这段代码中设定了线性布局的方向是vertical,添加了三个按钮,效果:


然后我们将方向设定为horizontal就可以看到三个控件是水平排列的了。


<?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" >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="right"        android:text="按钮1" />    <Button        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="right"        android:text="按钮2" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="按钮3" /></LinearLayout>

通过修改或添加一些属性,能够使得控件发生不同的变化。按钮1通过android:layout_gravity=”right”使得整个控件的位置位于屏幕的右侧。

按钮2的宽度修改为fill_parent同时通过android:gravity=”right”使得控件中的内容位于右侧。



布局中嵌套布局。



<?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" >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="right"        android:text="按钮1" />    <Button        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="right"        android:text="按钮2" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="按钮3" />    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:orientation="horizontal" >        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="按钮1" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="按钮2" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="按钮3" />    </LinearLayout></LinearLayout>

Layout_weight额外控件分配(权重)

Layout_weightSum

Layout_weightSum将布局中剩余的控件分为n份,单个控件通过Layout_weight设定值为x即可获得n份中的x份。


<?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" >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="right"        android:text="按钮1" />    <Button        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="right"        android:text="按钮2" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="按钮3" />    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:orientation="horizontal"         android:weightSum="2">        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="按钮1" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="按钮2" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="按钮3" />    </LinearLayout></LinearLayout><!-- Layout_weightSumLayout_weight额外空间分配(权重)visibility控制布局是否显示显示:visible不显示但占用空间:invisible隐藏:gone -->



更多相关文章

  1. Android(安卓)TextView文字超出一屏不能显示其它的文字 解决方案
  2. [Android(安卓)Pro] Android开发实践:为什么要继承onMeasure()
  3. Android开发笔记(一百二十)两种侧滑布局
  4. Android(安卓)WebView和EditText焦点冲突解决
  5. Android精通:TableLayout布局,GridLayout网格布局,FrameLayout帧布
  6. 使用Weditor(uiautomator2)替换uiautomatorviewer抓取Android控件
  7. Android图文列表实现(ListView)
  8. Android:listView控件,adapter,以及其他控件的组合
  9. android定制化显示toast

随机推荐

  1. 订单和产品的多对多表关系在crudapi系统
  2. 你必须掌握的 21 个 Java 核心技术
  3. 华为麒麟990 5G芯片重磅发布!全球首个旗舰
  4. 程序员最大的遗憾:在大学时忽略了数学
  5. 最强深度学习优化器Ranger开源:RAdam+Look
  6. “高级工程师”没用!你应该成为一名 “成
  7. 数据科学家年薪12万美元算高吗?我爬取近6
  8. 20201112 装饰器之函数即变量
  9. 0323作业-css基础知识3
  10. 使用grid实现一个12列栅格布局的组件,并g