Android五大布局和ConstraintLayout

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27747197/article/details/72377416

Android的五大基础布局为FrameLayout、LinearLayout、AbsoluteLayout、RelativeLayout和TableLayout,他们全部继承自ViewGroup.

FrameLayout(框架布局)

        框架布局是最简单的布局,Android中并没有对child view的摆布进行控制,这个布局中所有的控件都会默认出现在视图的左上角,我们可以使用android:Layout——margin和android:layout——gravity等属性来控制子控件相对布局的位置。

       LinearLayout(线性布局)

子控件为水平线性布局或者垂直线性布局,当有很多控件需要在一个界面中列出时,可以用LinearLayout布局,我们需要注意他的属性:android:orientation=“horizontal(水平)|vertical(垂直布局)”

AbsoluteLayoit(绝对布局)

可以放置多个控件,并且可以自定义控件的x,y位置。为了android的布局适配,我们基本上不用这中绝对布局。

RelativeLayout(相对布局)

这个布局也是相对自由的布局,Android对该布局的child view的之间可以设置相对位置来实现界面的布局。简单的讲就是字控件位于父控件的位置,例如layout_alignParentBottom,layout_alignParentTop,layout_alignParentLeft,layout_aliginParentRight等属性。还有子控件之间的相对布局,如layout_below,layout_above,layout_toRe

TableLayout(表格布局)

将子元素的位置分配到行或者列中,一个TableLayout由许多的TableRow组成。

ConstraintLayout(约束性布局)

这是一种最新的Android studio 的的推荐布局,使用这种布局,我们首先要添加项目依赖:compile 'com.android.support.constraint:constraint-layout:+”,官方对于constraint_layout的解释意思就是让你用一种灵活的方式布局子控件的位置和大小是 A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way.。意思就是让你用一种灵活的方式布局子控件的位置和大小。

Relative positioning(相对位置)

这个和RelativeLayout的用法有点相似。就是约束两个控件的相对位置


例如我们想实现如上图所示的相对布局,在代码中我们可以这样写

<Button android:id="@+id/buttonA" ... />
         <Button android:id="@+id/buttonB" ...
                 app:layout_constraintLeft_toRightOf="@+id/buttonA" />

Margins(边距)

下面就是各种边距,和我们其他布局的用法也差不多

  • android:layout_marginStart
  • android:layout_marginEnd
  • android:layout_marginLeft
  • android:layout_marginTop
  • android:layout_marginRight
  • android:layout_marginBottom

不过constraint-layout还可以margins when connected to a GONE widget,就是说如果其他相对控件不可见了,就是visibility为View.GONE,我们依旧可以设置边距,这样以前我们在开发过程中遇到过控件不可见导致布局变动的问题就可以很好的解决了

  • layout_goneMarginStart
  • layout_goneMarginEnd
  • layout_goneMarginLeft
  • layout_goneMarginTop
  • layout_goneMarginRight
  • layout_goneMarginBottom

Centering positioning and bias(居中位置和偏移)

例如我们设置了如下属性

<android.support.constraint.ConstraintLayout ...>
             <Button android:id="@+id/button" ...
                 app:layout_constraintLeft_toLeftOf="parent"
                 app:layout_constraintRight_toRightOf="parent/>
         </>
这样我们就可以实现控件相对于父控件居中对齐


我们还可以调整位置:

  • layout_constraintHorizontal_bias
  • layout_constraintVertical_bias
利用这两个属性,我们可以调整控件的位置类似边距,例如


我们如果想实现这样的布局,可用以下的代码,注意bias的数值是一个比例,上图中我们左边距是30%,默认的是50%,我们可以在代码中这样来写

<android.support.constraint.ConstraintLayout ...>
             <Button android:id="@+id/button" ...
                 app:layout_constraintHorizontal_bias="0.3"
                 app:layout_constraintLeft_toLeftOf="parent"
                 app:layout_constraintRight_toRightOf="parent/>
         </>

Visibility behavior(可见性行为)

子控件不加了之后,会被设置成一个点来保持相对控件之间的约束

Dimensions constraints(尺寸约束)

  • android:minWidth set the minimum width for the layout(设置最小的宽度)
  • android:minHeight set the minimum height for the layout(设置最小的高度)
我们还可以设置控件长宽的比率

 <Button android:layout_width="wrap_content"
                   android:layout_height="0dp"
                   app:layout_constraintDimensionRatio="1:1" />
还可以设置不行类型的chain(链)


更多有趣的用法可以看看这篇博客:http://blog.csdn.net/guolin_blog/article/details/53122387

猜你喜欢

转载自blog.csdn.net/qq_27747197/article/details/72377416
今日推荐