一.  概述

LinearLayout是线性布局组件,放置在其中的组件按列或者按行(就是垂直或者水平)的方式排序分布。

二. 常用XML配置属性

(1) android:orientation

设置LinearLayout容器布局组件的方式:只能取值:horizontal(水平的),vertical(垂直的)

(2) android:gravity

设置布局在LinearLayout容器内的组件对齐方式。

取值包括top, bottom, left, right, center, start, end(上,下,左,右,中,开始,结束)

(3)  View中继承来的属性

(包括android:background ,android:visibility等。还有一些改善美观的放置组件的间隔)

1. android:layout_widthandroid:layout_height (match_parent/wrap_content)

2 .android:layout_gravity   设置组件在容器中的布局 

3. android:layout_weight  设置组件占用空间的空余显示空间的比列

4. android:layout_margin  ,android:layout_marginTop  ,android:layout_marginBottom  ,android:layout_marginLeft  ,android:layout_marginRight 设置组件的外边界,类似我们搞网页设计HTML/CSS中margin用法。

三.  代码举例

activity_main.xml

<?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"android:orientation="vertical"><RadioGroupandroid:id="@+id/orientation"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="10dp"><RadioButtonandroid:id="@+id/horizontal"android:layout_width="wrap_content"android:layout_height="match_parent"android:text="水平"android:textSize="30dp"/><RadioButtonandroid:id="@+id/vertical"android:layout_width="wrap_content"android:layout_height="match_parent"android:text="垂直"android:textSize="30dp"/></RadioGroup><RadioGroupandroid:id="@+id/gravity"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:padding="10dp"><RadioButtonandroid:id="@+id/left"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="居左"android:textSize="30dp"/><RadioButtonandroid:id="@+id/center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="居中"android:textSize="30dp"/><RadioButtonandroid:id="@+id/right"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="居右"android:textSize="30dp"/></RadioGroup></LinearLayout>
MainActivity.java

package com.example.android_demo02;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.RadioGroup;public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {private RadioGroup orientation;private RadioGroup gravity;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);orientation=(RadioGroup) findViewById(R.id.orientation);orientation.setOnCheckedChangeListener(this);gravity=(RadioGroup) findViewById(R.id.gravity);gravity.setOnCheckedChangeListener(this);}@Overridepublic void onCheckedChanged(RadioGroup radioGroup, int i) {switch (i){case R.id.horizontal:orientation.setOrientation(LinearLayout.HORIZONTAL);break;case R.id.vertical:orientation.setOrientation(LinearLayout.VERTICAL);break;case R.id.left:gravity.setGravity(Gravity.START);break;case R.id.center:gravity.setGravity(Gravity.CENTER_HORIZONTAL);break;case R.id.right:gravity.setGravity(Gravity.END);break;}}}

四. 实现效果:

【Android容器组件—LinearLayout】相关推荐

  1. 【Android容器组件—ScrollView】

    一. 概述 ScrollView也是一个容器,它是FrameLayout的子类,它的主要作用就是将超出物理屏幕的内容显示出来,(就是滚动条效果)ScrollView提供垂直滚动,进而可将超出物理屏幕的 ...

  2. android线性布局设置控件固定在底部,Android UI组件LinearLayout线性布局详解

    LinearLayout 线性布局,该布局的继承关系: 1. 什么是线性布局 通俗的说感觉起来和线有关,参照线的特点,有么是横向的,要么是竖向的. LinearLayout是线性布局控件,它包含的子控 ...

  3. Android Jetpack组件之Navigation使用-源码

    1.前言 最近简单看了下google推出的框架Jetpack,感觉此框架的内容可以对平时的开发有很大的帮助,也可以解决很多开发中的问题,对代码的逻辑和UI界面实现深层解耦,打造数据驱动型UI界面. A ...

  4. Android——四大组件、六大布局、五大存储

    一.android四大组件 (一)android四大组件详解 Android四大组件分别为activity.service.content provider.broadcast receiver. 1 ...

  5. XCoreRedux框架:Android UI组件化与Redux实践

    XCoreRedux框架:Android UI组件化与Redux实践 @author: 莫川 https://github.com/nuptboyzhb/ XCoreRedux源码+Demo:http ...

  6. Flutter学习笔记(10)--容器组件、图片组件

    如需转载,请注明出处:Flutter学习笔记(10)--容器组件.图片组件 上一篇Flutter学习笔记(9)--组件Widget我们说到了在Flutter中一个非常重要的理念"一切皆为组件 ...

  7. 【android编程】第五讲-Android高级组件

    Android第五讲高级组件 实践题Code链接:https://blog.csdn.net/xbean1028/article/details/104771559 文章目录 Android第五讲高级 ...

  8. Android 四大组件,五大存储,六大布局

    Android 四大组件 android四大组件分别是:Activity, service,content provider,broadcast receiver 一.Activity 1.概念: a ...

  9. Android开发之LinearLayout布局

    文章目录 常见属性 实例 设置容器的对齐与排列方式 设置分割线 设置权重分配空间 常见属性 orientation 布局中组件的排列方式,分为垂直和水平 gravity 控制组件所包含的子元素的对齐方 ...

最新文章

  1. 数秒植入木马,一击即破,你的DNN模型还安全吗?
  2. Gartner公布2018十大技术发展趋势 2025年AI重塑商业模式
  3. java B2B2C Springcloud电子商城系统-断路器(Hystrix)
  4. 累加器配上委托也可以很吊
  5. JavaScript模块化开发整理
  6. 利用MATLAB实现移动目标的TDOA/FDOA定位算法
  7. 搭建基于.NetFrameWork的私有nuget服务端及打包项目发布上传
  8. Web常见漏洞修复建议
  9. 试述计算机控制系统的大致组成,试述工业计算机控制系统的组成及应用
  10. 能源36号文解读_财税2016年36号文件全文解读【专业分析】
  11. CentOs安装pyhive
  12. 【爬虫】问题集-豆瓣电影
  13. libxml2的参考手册
  14. Redis Expire 设置key过期时间
  15. C语言循环输出能被3整除的数
  16. eclipse打开时报错:
  17. MySQL中的事务相关属性以及JDBC编程
  18. stm32f103vet6利用fsmc控制LIL9341液晶屏
  19. YOLOV8:FileNotFoundError: train: No labels found in /home/smy/new-yolov5/ultralytic
  20. 长春理工大学计算机考研资料汇总

热门文章

  1. 在C#中使用WIA获取扫描仪数据
  2. 人一生要读的100首古诗
  3. 比特大陆之争终落幕,吴忌寒为爱和平退出?
  4. 南宁琅东的机器人餐厅_南宁这几家私房菜藏得深,人气却只增不减,真是菜香不怕巷子深~...
  5. python学习---画3d曲线曲面图
  6. 编写网易邮箱登陆界面
  7. Tapestry入门
  8. 一次完整的HTPPS请求
  9. 一篇关于数学建模美赛论文撰写的心得
  10. [转载]hostapd配置解析