Android中LinearLayout布局的常用属性总结

作者:chris930921 时间:2023-11-23 17:09:37 

基本属性要求


<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"

android:orientation="vertical">
</LinearLayout>

  • android:orientation

  • 决定是水平排列或是垂直排列

  • vertical 垂直排列

  • horizontal 水平排列

垂直排列 Button


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

</LinearLayout>

水平排列 Button


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

</LinearLayout>

重心设定


<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"

android:gravity="left">
</LinearLayout>

  • android:gravity

  • 设定框架的内容的放置方向

  • center 水平垂直皆置中

  • center_vertical 垂直置中

  • center_horizontal 水平置中

  • top 置顶

  • left 置左

  • bottom 置底

  • right 置右

水平、垂直置中


<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:gravity="center_vertical">

<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Button 1" />

</LinearLayout>

<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:gravity="center_horizontal">

<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Button 1" />

</LinearLayout>

<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:gravity="center">

<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Button 1" />

</LinearLayout>

透过 OR 运算子组合重心


<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:gravity="top|right">

<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Button 1" />

</LinearLayout>

<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:gravity="bottom|left">

<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Button 1" />

</LinearLayout>

<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:gravity="center_vertical|center_horizontal">

<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Button 1" />

</LinearLayout>

比例分配


<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="horizontal">

<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Button 1"

android:layout_weight="1"/>

</LinearLayout>

  • android:layout_weight

  • 子元件或子框架的比重。

  • LinearLayout 下的子元件或子框架,才能设定这项属性。

等比例分配


<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="horizontal">

<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Button 1"
   android:layout_weight="1"/>

<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Button 2"
   android:layout_weight="1"/>

<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Button 3"
   android:layout_weight="1"/>

</LinearLayout>

比重都是 1,所以大小相同。


非等比例分配


<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="horizontal">

<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Button 1"
   android:layout_weight=".10"/>

<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Button 2"
   android:layout_weight=".20"/>
 <Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Button 3"
   android:layout_weight=".70"/>

</LinearLayout>

.10 代表 0.10
.20 代表 0.20
.70 代表 0.70
合起来刚好是 1 ,作 100% 分配。      

标签:Android,LinearLayout
0
投稿

猜你喜欢

  • Android 开发之Dialog,Toast,Snackbar提醒

    2021-05-31 14:27:20
  • 推荐两款java开发实用工具 hutool 和 lombok

    2022-04-06 10:20:14
  • Android 获取判断是否有悬浮窗权限的方法

    2023-11-04 22:48:18
  • java中的常用集合类整理

    2022-08-22 10:35:49
  • 通过实例了解Java 8创建Stream流的5种方法

    2023-04-13 22:41:02
  • C#重载运算符详解

    2023-10-27 13:35:18
  • C#将Sql数据保存到Excel文件中的方法

    2023-11-11 08:34:26
  • C#实现图形区域组合操作的方法

    2023-05-01 19:08:21
  • Mac OS下为Android Studio编译FFmpeg解码库的详细教程

    2023-06-30 02:37:54
  • Android实现GridView中的item自由拖动效果

    2022-02-15 17:50:15
  • Unity实现汽车前后轮倒车轨迹计算

    2022-12-18 17:49:47
  • java识别一篇文章中某单词出现个数的方法

    2022-04-24 06:18:40
  • java集合框架的体系结构详细说明

    2021-10-24 10:33:29
  • C# wpf Grid中实现控件拖动调整大小的示例代码

    2023-05-15 17:12:03
  • android照相、相册获取图片剪裁报错的解决方法

    2021-11-21 20:18:14
  • List转换成DataSet实现代码

    2023-03-02 04:46:01
  • 使用C#实现读取系统配置文件的代码实例讲解

    2023-02-18 10:36:56
  • 实例详解Android解决按钮重复点击问题

    2023-10-16 09:10:12
  • Java中如何调用cmd压缩文件

    2023-12-09 13:37:02
  • Java邮件发送程序(可以同时发给多个地址、可以带附件)

    2022-09-07 20:08:40
  • asp之家 软件编程 m.aspxhome.com