Android笔记2(布局管理器)

1.View与ViewGroup:
Android的UI界面都是由View和ViewGroup及其派生类构建而成的。其中,View视图,是用户接口组件的基本构建块,它在屏幕中占用一个矩形区域,它是所有UI控件的基类,比如 TextView, ImageView。而ViewGroup是容纳View及其派生类的容器,是一个特殊的View,能够容纳其它的View(子控件),它是布局和视图容器的基类。ViewGroup作为容器盛装界面的控件,可以包含普通的View控件,也可以包含 ViewGroup。
注意:Android应用的每个界面的根元素必须有且仅有一个ViewGroup容器。

2.Android布局管理器
(1)在Android程序中界面是通过布局文件设定的,在每个应用程序创建时,会默认包含一个主界面布局,该布局位于res/layout目录中。

(2)

 (3)Android布局的通用属性
五种常用布局直接或者间接继承ViewGroup,因此这五种常用布局也支持在ViewGroup中定义属性,这些属性可以看作是布局的通用属性。

 (4)线性布局
(1 线性布局(LinearLayout)主要以水平或垂直方式来显示界面中的控件。
当控件水平排列时,显示顺序依次为从左到右,当控件垂直排列时,
显示顺序依次为从上到下。
注意:子控件之间不能重叠,可以设置权重。

2)LinearLayout布局中的 android:layout_width属性值不可设为wrap_content。这是因为LinearLayout的优先级比Button高,如果设置为wrap_content,则Button控件的android:layout_weight属性会失去作用。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"android:layout_height="wrap_conten" android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
andrpid:layout_weight="1" 
android:text=" 按钮 1"/>
………
</LinearLayout>

注意:当控件使用权重属性时,布局宽度属性值通常设置为0dp

3)RelativeLayout(相对布局)通过相对定位的方式指定子控件的位置。在XML布局文件中定义相对布局时使用<RelativeLayout>标签,基本语法格式:
 

<RelativeLayout 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" tools:context=".MainActivity">
 ....
</RelativeLayout>

 ①根据父容器定位
在相对布局中,可以通过以下的属性让的组合让控件处于父容器左上角、右上角、左下角、右下角、上下左右居中,正居中等九个位置。属性如下:

android:layout_alignParentLeft="true" 父容器左边
android:layout_alignParentRight="true" 父容器右边
android:layout_alignParentTop="true" 父容器顶部
android:layout_alignParentBottom="true" 父容器底部
android:layout_centerHorizontal="true" 水平方向居中
android:layout_centerVertical="true" 垂直方向居中
android:layout_centerInParent="true" 水平垂直都居中

 ②根据兄弟控件定位
在相对布局中,还支持通过已确定位置的控件作为参考来确定其他控件的位置,以下的属性让的组合让控件处于另外控件左上角、右上角、左下角、右下角、正上方、正下方、正左方、正右方等位置。属性如下:

android:layout_toLeftOf="@+id/button1" 在button1控件左方
android:layout_toRightOf="@+id/button1" 在button1控件右方
android:layout_above="@+id/button1" 在button1控件上方
android:layout_below="@+id/button1" 在button1控件下方
android:layout_alignLeft="@+id/button1" 与button1控件左边平齐
android:layout_alignRight="@+id/button1" 与button1控件右边平齐
android:layout_alignTop="@+id/button1" 与button1控件上边平齐
android:layou_alignBottom="@+id/button1" 与button1控件下边平齐

 (5)约束布局
1)约束布(ConstraintLayout)是解决布局嵌套过多的问题,以灵活的方式定位和调整小部件。
①ConstraintLayout是Android Studio 2.2新添加的布局。
②ConstraintLayout布局中的控件可以在横向和纵向上以添加约束关系的方式进行相对定位,其中,横向边包括Left、Start 、Right 、End,纵向边包括Top、Bottom、Baseline (文本底部的基准线)。

2)

  3)例如,下面的代码将使左侧的偏移量为30%,而不是默认的50%,这样左侧就会更短,控件会更倾向于左侧。

<androidx.constraintlayout.widget.ConstraintLayout...>
<Button android:id="@+id/button"
app:layout_constraintHorizontal_bias="0.3"
app:layout_constraintLeft_toLeft0f="parent"
app:layout_constraintRight_toRight0f="parent/>

 

4)Chain(链)是一种特殊的约束,它使我们能够对一组水平或竖直方向互相关联的控件进行统一管理。一组控件通过一个双向的约束关系链接起来,就能形成一个 Chain。

①链头:
链由在链的第一个元素(链的“head”)上设置的属性控制:

 

 

 head是水平链的最左侧控件,垂直链的最顶部控件。
②链样式
当在链的第一个元素上设置属性layout_constraintHorizontal_chainStyle或layout_constraintVertical_chainStyle时,链的行为将根据指定的样式改变(默认为CHAIN_SPREAD)。
1. CHAIN_SPREAD元素将被展开(默认样式)
2. 权重链在CHAIN_SPREAD模式下,如果一些小部件被设置为
3. MATCH_CONSTRAINT,它们将分割可用空间
4. CHAIN_SPREAD_INSIDE类似,但是链的端点不会展开
5. CHAIN_PACKED链的元素将被打包在一起。子元素的水平或垂直偏置属性将影响打包元素的位置。 

③链新式

 ④权重链

链的默认行为在可用空间中均匀分布元素。如果一个或多个元素使用了MATCH_CONSTRAINT,它们将使用可用的空空间(在它们之间平均分配)。
属性layout_constraintHorizontal_weight 和layout_constraintVertical_weight将使用MATCH_CONSTRAINT控制空间如何在元素之间分布。例如,在一个使用MATCH_CONSTRAINT包含两个元素的链上,第一个元素的权值为2,第二个元素的权值为1,第一个元素所占用的空间将是第二个元素的两倍。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值