LinearLayout初步了解

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情
布局:
    1、文件位置res/layout/***.xml
    2、确定布局管理器类型
    3、在布局管理器中添加需要的组件
    4、修改activity里指向的布局文件
    5、所有的资源文件都必须小写

LinearLayout:
    特点:每一行或者每一列中,只能放一个组件,当组件一个一个的排列到窗体的边缘后,剩下的组件不会被显示。在垂直线性布局管理器中,每一行只能放一个组件,在水平线性布局管理器中,每一列只能放一个组件。
    方式:XML配置或者JAVA代码 推荐XML配置
    常用属性:
        android:orientation="horizontal | vertical":vertical垂直的,horizontal水平的
        android:layout_width="wrap_content | match_parent": 包裹内容 | 填充父类空间
        android:layout_height="match-parent":同上(fill_parent不推荐使用)
        android:id="@+id/名称":设置id这个属性,可以在java文件中取到布局
        android:background:图片或者颜色
        android:gravity:控制布局控件中的对齐方式。如果没有子控件的控件设置此属性,表示其内容的对齐方式,比如说TextView里面文字的对齐方式,若有子控件的控件设置此属性,表示其子控件的对其方式,gravity如果需要设置多个属性,中间用"|"进行组合。
        android:layout_weight="1":通过设置控件的layout_weight属性以控制各个控件在布局中的相对大小,线性布局会根据该控件layout_weight值与其所处布局中的所有控件layout_weight值之和的比值为该控件分配占用的区域。如果layout_weight值为0,控件会按原大小显示,不会被拉伸,对于其他layout_weight属性值大于0的控件,系统将会减去属性值为0的元素所占位置的大小,然后剩余的按比例分配
    android:visibility=""visible | invisible | gone":显示 | 不显示但会占位置 |  不宣示也不占位置 

android:gravity与android:layout_gravity的区别:
    android:gravity:是指本元素的子元素相对它的对齐方式
    android:layout_gravity:是指本元素相对它的父元素的对齐方式。

android:layout_margin***:设置本控件距离父控件的个数
android:layout_padding***:设置子控件距离父控件的距离
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
 
<LinearLayout
android:id="@+id/ll_up"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#ff00ff"
android:gravity="top"
android:orientation="horizontal"
android:padding="4dp" >
 
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="第一列" />
 
<Button
android:id="@+id/btn_close"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_weight="1"
android:gravity="bottom|center"
android:padding="5dp"
android:text="关闭" />
 
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="第二列" />
</LinearLayout>
 
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
</LinearLayout>
 
</LinearLayout>
利用java代码实现相同效果,不推荐
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
 
public class MainActivity extends Activity {
 
private LinearLayout ll_parent;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
 
// 获得父控件
ll_parent = (LinearLayout) findViewById(R.id.ll_parent);
 
// 新建新的LinearLayout控件
LinearLayout ll_up = new LinearLayout(this);
 
// 新建LinearLayout属性对象
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT, 1);
 
// 添加属性
ll_up.setLayoutParams(params);
 
// 设置方向
ll_up.setOrientation(LinearLayout.HORIZONTAL);
 
// 添加图片资源
ll_up.setBackgroundResource(R.drawable.fuck);
 
// 添加按钮1
// 新建一个按钮
Button btn_01 = new Button(this);
// 设置按钮的字体
btn_01.setText("按钮1");
// 新建按钮的属性
LayoutParams params2 = new LayoutParams(150, LayoutParams.MATCH_PARENT);
// 把属性添加到按钮中
btn_01.setLayoutParams(params2);
// 把按钮添加到父控件中
ll_up.addView(btn_01);
 
// 添加按钮
Button btn_02 = new Button(this);
btn_02.setText("按钮2");
btn_02.setLayoutParams(params2);
ll_up.addView(btn_02);
 
// 添加按钮
Button btn_03 = new Button(this);
btn_03.setText("按钮3");
btn_03.setLayoutParams(params2);
ll_up.addView(btn_03);
 
// 父控件中添加这个控件
ll_parent.addView(ll_up);
 
LinearLayout ll_down = new LinearLayout(this);
ll_down.setLayoutParams(params);
ll_down.setBackgroundResource(R.drawable.lj0601bz11);
 
ll_down.setOrientation(LinearLayout.VERTICAL);
 
// 添加按钮
Button btn_04 = new Button(this);
btn_04.setText("按钮4");
// 这次用的是父类型的类型
LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1);
btn_04.setLayoutParams(params4);
ll_down.addView(btn_04);
 
// 添加按钮
Button btn_05 = new Button(this);
btn_05.setText("按钮5");
btn_05.setLayoutParams(params4);
ll_down.addView(btn_05);
 
// 添加按钮
Button btn_06 = new Button(this);
btn_06.setText("按钮6");
btn_06.setLayoutParams(params4);
ll_down.addView(btn_06);
 
ll_parent.addView(ll_down);
 
}
 
}
LinearLayout源码解析 为什么学习自从学了Android自定义控件的一些知识,总是处于似懂非懂状态,说都说了上来,自己在项目里封装了一些自定义控件,但是还是缺乏一个很直观的了解。所以去了解学习下Android是如何封装控件的,就从简单的入手,分析下LinearLayout是如何实现的什么是LinearLayout作为最基础的布局,所以从事过Android开发的同学都应该非常了解 中文解释应该叫做线性布局,相比如Relat 阅读详情

相关推荐

android linearlayout代替listview实现

最近在写项目,遇到了一个问题,需要在一个页面中显示两个列表

未来尚未知的博客 2289

Android 线性布局(LinearLayout)内各控件如何设置间距

开门见山 解决问题 在线性布局中无论orientation属性设为竖直还是水平都可以用layout_margin属性来控制布局内控件的间距android:layout_marginTop="10dp"// 当前控件上边缘与其他控件(布局)的间距 android:layout_marginBottom="10dp" //当前控件下边缘与其他控件(布局)的间距 android:layout...

qq_41297896的博客 3万+

Android常用Layout源码总结—LinearLayout

文章目录前言LinearLayout特点源码探究布局属性LayoutParamsonMeasure测量开始测量准备初始参数阶段预测量阶段补充测量阶段测量尾声阶段onLayout布局onDraw绘制总结 前言 通过学习Android官方Layout的源码,可以帮助自己更好的理解Android的UI框架系统,了解内部便捷的封装好的API调用,有助于进行布局优化和自定义view实现等工作。这里把学习结果...

chidh的博客 815

Linearlayout 中设置子View之间的空白间距

在做Android 项目中经常会使用 margin 来设置 view之间的间距非常的简单也很方便 并且还可以 通过layoutparams动态的来设置 但是在动态设置并不是值得提倡的 今天学到来一个新的方法 就是在linearlayout 中 通过 divider属性来设置 间距 当然 divider 只会接受一个图片 所以想要什么样式的间距 可以通过 shape 来自己设定 就可以 方便简单

haoxuezhe1988的专栏 6333

Android中的布局——LinearLayout「布局属性、设置控件、边距、位置」

Android中的常用布局 布局的基本应用 布局的常用公有属性 设置控件宽高 设置控件外边距 区分内、外边距 说明: 外边距是指布局和屏幕边缘之间的距离 内边距是指布局中的控件和与布局之间的距离 设置控件相对位置 线性布局LinearLayout的使用 对控件默认按照水平方向进行排列,但是当控件的个数超出布局的范围的时候是不会自动换行的,多出的内容会被压缩或者显示不出来。 水平方向:android:orientation=“horizontal” 垂直方向:android:orientat

hey,欢迎浏览我的博客,希望能给你提供帮助。 9709

07. 【Android教程】Android 线性布局 LinearLayout

在上一节中,我们讲到了所有的 Layout 都是从 ViewGroup 继承而来,它可以包含若干 View 并按照指定的规则将这个 View 摆放到屏幕上。

这是一个web全栈开发的博客,取其精华去其糟粕,争取让大家一看就懂,一学就会,一用就成~ 2706

android】巧用android:divider属性设置LinearLayout中元素之间的间隔

如上图,要想实现3个button线性排列并且使它们的大小相同、间隔相等、而且整体填充满整个linearlayout,我们一般的做法是在每两个button之间放一个固定宽度的view,然后设置button的宽度为0、layout_weight为1。这样虽能实现功能,可是总感觉不方便,特别是button多的时候。 今天介绍另一种简单、优雅的方法,就是利用android:divider属性。

u011494050的专栏 2万+

Activity的布局初步(二)嵌套布局【LinearLayout、TableLayout】

本文通过两个示例说明嵌套布局 示例一:     将Activity界面分成上、下2部分,然后上部分是用横向的(水平)布局,里面有4个TextView ,下部分则是用纵向的(垂直)布局,也放有4个TextView。 要实现这样的布局必须要使用到嵌套布局。 实现步骤: 1、 首先,最外层是一个垂直布局的LinearLayout; 2、 在最外层的LinearLayout中再嵌套

LO/QI 2850

LinearLayout

LinearLayout 基础

Android_yh的博客 2万+

LinearLayout详解一:LinearLayout的简介

打蛇要打七寸,做事情要做要害,看东西要看本质。LinearLayout的本质,也就是他的最上层的类,其实是View类

kyson 3837

androidLinearLayout中动态添加View

androidLinearLayout中动态添加View前言了解使用参考完事 前言 在项目中有一个列表,列表里的item里面还有一个未知个数的横向列表 用列表来做,RecyclerView、ListView、GridView都需要另外再写一个Adapter,显得麻烦。这时候直接用一个横向的LinearLayout来使用addView方法就很方便了 了解 addView是ViewGroup中特...

杨迪龙的博客 1万+

Activity布局初步 LinearLayout和TableLayout

1.LinearLayout android:orientation="vertical"或者"horizontal" 其实这个就是一个一个的控件定义,顺序垂直或者水平地进行排列 android:id 为控件指定相应的id android:text 指定控件当中显示的文字,需要注意的是,尽量使用strings.xml android:gravity 指定控件的基本位置,比如居中,居右

superhill的学习笔记 199

LinearLayout的onMeasure总结

void measureVertical(int widthMeasureSpec, int heightMeasureSpec) { mTotalLength = 0; int maxWidth = 0; int childState = 0; int alternativeMaxWidth = 0; int weig

gggggg0023的专栏 1839

深入了解LinearLayout

主要内容: 1 LinearLayout布局的嵌套 2 奇葩的layout_weight属性 如何实现下面的嵌套布局? 上图,最外面是一个水平的linearLayout,水平的linearLayout里面又嵌套了两个linearlayout,这两个嵌套的linearlayout是一个垂直的linearLayout,里面各有两个TextView控件。 实现如下: <Li

longshan_2009的专栏 626

LinearLayout 内部 挂件 居中

LinearLayout 内部 挂件 居中 Posted by androidfoot on August 10, 2010 Leave a comment (0)Go to comments 直接设置LinearLayout内部挂件 :android:layout_g

1万+

关于linearlayout

LinearLayout----Layout that arranges its children in a single column or a single row. Linear--线性的,直线的意思 Layout--布局,规划 那么LinearLayout应该就是以线性的方式对它的组件进行布局的吧,只能以横向或纵向的方向规划组件,^_^  Ex1: <Linear

gsldqtan的专栏 604

如何在代码中设置LinearLayout的高度

如何在代码中设置LinearLayout的高度 main.xml声明 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/context" android:layout_width="match_parent" android:layout_h

androiddeveloper的专栏 2万+

7 线性布局——LinearLayout

在上一节中,我们讲到了所有的 Layout 都是从 ViewGroup 继承而来,它可以包含若干 View 并按照指定的规则将这个 View 摆放到屏幕上。那么接下来的章节我们就来学习一下 Android 的 UI 布局,Android 原生有六大布局,分别是: LinearLayout(线性布局)、RelativeLayout(相对布局)、TableLayout(表格布局)、FrameLayout(帧布局)、AbsoluteLayout(绝对布局)、GridLayout(网格布局),我们从最简单实用的一个

MC 的技术空间 2738

Linearlayout添加监听事件

今天用给LinearLayout添加监听,并按下改变LinearLayout的背景色,好了,废话不多说! 这是我的LinearLayout布局: descendantFocusability属性:         beforeDescendants:viewgroup会优先其子类控件而获取到焦点         afterDescendants:viewgr

joxpeng的专栏 6204
上一篇: RelativeLayout初步了解
下一篇: DrawerLayout初步了解
Yo_Hack
博客等级 码龄12年 2粉丝 44原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值