Android布局之LinearLayout线性布局

Android布局之LinearLayout线性布局,第1张

概述LinearLayout是线性布局控件:要么横向排布,要么竖向排布常用属性:android:gravity------------设置的是控件自身上面的内容位置

linearLayout是线性布局控件:要么横向排布,要么竖向排布

常用属性:

androID:gravity------------设置的是控件自身上面的内容位置

androID:layout_gravity-----设置控件本身相对于父控件的显示位置

androID:layout_weight----- 给控件分配剩余空间

先给大家展示一下导图:


知识点详解(演示效果方便组件没有设置ID)

(1)gravity和Layout_gravity

androID:gravity 属性是对该vIEw中内容的限定.比如一个button 上面的text. 你可以设置该text 相对于vIEw的靠左,靠右等位置.

androID:layout_gravity是用来设置该vIEw相对与父vIEw 的位置.比如一个button 在linearlayout里,你想把该button放在linearlayout里靠左、靠右等位置就可以通过该属性设置.

(2)weight权重(以水平为例)

  (a)当wIDth = 0或者 wIDth = wrap_content的时候,按照权重比例计算!:2: 3

<?xml version="1.0" enCoding="utf-8"?><linearLayoutxmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:orIEntation="horizontal"><TextVIEwandroID:ID="@+ID/text1" androID:layout_wIDth="0dp"androID:layout_height="match_parent"androID:layout_weight="1"androID:background="@androID:color/holo_red_dark"androID:text="Text1"/><TextVIEwandroID:ID="@+ID/text2"androID:layout_wIDth="0dp"androID:layout_height="match_parent"androID:layout_weight="2"androID:background="@androID:color/holo_blue_bright"androID:text="Text2"/><TextVIEwandroID:ID="@+ID/text3"androID:layout_wIDth="0dp"androID:layout_height="match_parent"androID:background="@androID:color/white"androID:layout_weight="3"androID:text="Text3"/></linearLayout> 

  (b)当wIDth = fill_parent/match_parent的时候

    第一步:当三个都为match_parent的时候屏幕只有一个 1 -3 = -2;

    第二步:计算每个TextVIEw占有的比例 1/6,2/6,3/6;

    第三步: 1 -2*1/6 = 2/3; 1 - 2*2/6 = 1/3; 1 - 2*3/6 = 0;

    第四步:2:1:0

<?xml version="1.0" enCoding="utf-8"?><linearLayoutxmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:orIEntation="horizontal"><TextVIEwandroID:ID="@+ID/text1"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:layout_weight="1"androID:background="@androID:color/holo_red_dark"androID:text="Text1"androID:gravity="center"androID:textSize="40sp"/><TextVIEwandroID:ID="@+ID/text2"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:layout_weight="2"androID:background="@androID:color/holo_blue_bright"androID:text="Text2"androID:gravity="center"androID:textSize="40sp"/><TextVIEwandroID:ID="@+ID/text3"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:background="@androID:color/white"androID:layout_weight="3"androID:text="Text3"androID:gravity="center"androID:textSize="40sp"/></linearLayout> 

(3)分割线

<VIEwandroID:layout_marginleft="20sp"androID:layout_marginRight="20sp"androID:layout_wIDth="3"androID:layout_height="match_parent" androID:background="#ff00ee" /> 

案例(底部导航)

<?xml version="1.0" enCoding="utf-8"?><linearLayoutxmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="match_parent"androID:layout_height="match_parent" androID:orIEntation="horizontal"><linearLayoutandroID:layout_wIDth="match_parent"androID:layout_height="80sp"androID:layout_gravity="bottom"androID:background="@androID:color/holo_purple"><linearLayoutandroID:layout_wIDth="0sp"androID:layout_height="match_parent"androID:layout_weight="1"androID:orIEntation="vertical"androID:gravity="center"><ImageVIEwandroID:layout_wIDth="60dp"androID:layout_height="60dp"androID:src="@mipmap/ic_launcher"/><TextVIEwandroID:layout_wIDth="wrap_content"androID:layout_height="wrap_content"androID:text="one"androID:textSize="20sp"/></linearLayout><VIEwandroID:layout_marginleft="20sp"androID:layout_marginRight="20sp"androID:layout_wIDth="3"androID:layout_height="match_parent"androID:background="#ff00ee" /><linearLayoutandroID:layout_wIDth="0sp"androID:layout_height="match_parent"androID:layout_weight="1"androID:orIEntation="vertical"androID:gravity="center"><ImageVIEwandroID:layout_wIDth="60dp"androID:layout_height="60dp"androID:src="@mipmap/ic_launcher"/><TextVIEwandroID:layout_wIDth="wrap_content"androID:layout_height="wrap_content"androID:text="two"androID:textSize="20sp"/></linearLayout><VIEwandroID:layout_marginleft="20sp"androID:layout_marginRight="20sp"androID:layout_wIDth="3"androID:layout_height="match_parent"androID:background="#ff00ee" /><linearLayoutandroID:layout_wIDth="0sp"androID:layout_height="match_parent"androID:layout_weight="1"androID:orIEntation="vertical"androID:gravity="center"><ImageVIEwandroID:layout_wIDth="60dp"androID:layout_height="60dp"androID:src="@mipmap/ic_launcher"/><TextVIEwandroID:layout_wIDth="wrap_content"androID:layout_height="wrap_content"androID:text="three"androID:textSize="20sp"/></linearLayout></linearLayout></linearLayout>

以上内容给大家介绍了AndroID布局之linearLayout线性布局的相关知识,希望大家喜欢。

总结

以上是内存溢出为你收集整理的Android布局之LinearLayout线性布局全部内容,希望文章能够帮你解决Android布局之LinearLayout线性布局所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1141672.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存