当前位置:   article > 正文

Android布局——LinearLayout 线性布局

linearlayout

简介

贴上官方文档:线性布局  |  Android 开发者  |  Android Developers

Linear 意为 线形的,线状的,所以该布局具有线的特点,要么是横向的,要么是竖向的。

该布局通过android:orientation (orientation:定位) 属性来设置布局内子控件是纵向还是横向,超过边界时,某些控件将消失。

常用属性

属性

android:orientation ——规定布局内子控件纵向还是横向

常量值描述
horizontal0水平布局
vertical1垂直布局

android:layout_height ——指定该控件的的高度

android:layout_width ——指定该控件的宽度

注:该控件的值也可以自行设置常数

常量值描述
match_parent-1该控件大小和父级控件一样大
wrap_content-2以该控件的内容为准的大小,即内容有多大就有多大

android:layout_weight ——指定该控件所占的权重 *

android:gravity ——指定控件内内容如何定位

常量值描述
bottom50把对象堆到容器的底部,不改变大小
center11

把对象放在容器的中心(水平垂直),不改变大小

center_horizontal1水平居中
center_vertical10垂直居中
fill77完全填满容器
left3推到容器左侧
right5推到容器右侧
top30推到容器顶部
start800003推到容器开头
end800005推到容器末尾

布局权重

LinearLayout中可以用android:layout_weight来分配权重。此属性会根据视图应在屏幕上占据的空间大小,向视图分配“重要性”值。如果拥有更大的权重值,视图便可展开,填充父视图中的任何剩余空间。子视图可指定权重值,然后系统会按照子视图所声明的权重值比例,为其分配视图组中的任何剩余空间。默认权重为零。

均等分布

想要让每一个子视图都使用相同大小的空间,就必须把每个视图的android:layout_weight的值设置为1。

且如果是水平布局,则把每个视图的android:layout_width设为0dp;如果是垂直布局,则把每个视图的android:layout_height设为0dp。

示例如下:

水平布局

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="horizontal"
  8. android:gravity="center"
  9. tools:context=".MainActivity">
  10. <TextView
  11. android:layout_width="0dp"
  12. android:layout_height="wrap_content"
  13. android:layout_weight="1"
  14. android:background="@color/purple_200"
  15. android:text="12345"/>
  16. <TextView
  17. android:layout_width="0dp"
  18. android:layout_height="wrap_content"
  19. android:layout_weight="1"
  20. android:background="@color/teal_200"
  21. android:text="12345678910abcdefg"/>
  22. </LinearLayout>

结果如图所示:

垂直布局

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. android:gravity="center"
  9. tools:context=".MainActivity">
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="0dp"
  13. android:layout_weight="1"
  14. android:background="@color/purple_200"
  15. android:text="12345"/>
  16. <TextView
  17. android:layout_width="wrap_content"
  18. android:layout_height="0dp"
  19. android:layout_weight="1"
  20. android:background="@color/teal_200"
  21. android:text="12345"/>
  22. </LinearLayout>

结果如图所示:

不等分布 

 通过分配权重,权重数越大,代表该视图越重要,所占空间就越多。

如果有三个文本视图,左右两边声明为1,中间声明为2,则代表中间的视图更加重要,所占空间更多。

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="horizontal"
  8. android:gravity="center"
  9. tools:context=".MainActivity">
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_weight="1"
  14. android:background="@color/purple_200"
  15. android:text="111"/>
  16. <TextView
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_weight="2"
  20. android:background="@color/pink"
  21. android:text="222"/>
  22. <TextView
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_weight="1"
  26. android:background="@color/teal_200"
  27. android:text="333"/>
  28. </LinearLayout>

结果如图所示:

常用属性示例

 纵向布局

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".MainActivity">
  9. <TextView
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:text="1111111"
  13. android:background="@color/teal_200" />
  14. <TextView
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:text="2222222"
  18. android:background="@color/purple_200" />
  19. <TextView
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:text="3333333"
  23. android:background="@color/white" />
  24. <TextView
  25. android:layout_width="match_parent"
  26. android:layout_height="wrap_content"
  27. android:text="4444444"
  28. android:background="@color/purple_700" />
  29. </LinearLayout>

 横向布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="horizontal"
  8. android:gravity="center"
  9. tools:context=".MainActivity">
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="match_parent"
  13. android:text="1111111"
  14. android:background="@color/teal_200" />
  15. <TextView
  16. android:layout_width="wrap_content"
  17. android:layout_height="match_parent"
  18. android:text="2222222"
  19. android:background="@color/purple_200" />
  20. <TextView
  21. android:layout_width="wrap_content"
  22. android:layout_height="match_parent"
  23. android:text="3333333"
  24. android:background="@color/white" />
  25. <TextView
  26. android:layout_width="wrap_content"
  27. android:layout_height="match_parent"
  28. android:text="4444444"
  29. android:background="@color/purple_700" />
  30. </LinearLayout>

参考博客:Android-0.Android Studio布局中layout_weight用法_花熊的博客-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/642622
推荐阅读
相关标签
  

闽ICP备14008679号