Android常用布局小结

ConstraintLayout(推荐)

注意:

  • 每个视图都必须至少有两个约束条件:一个水平约束条件,一个垂直约束条件。

  • 只能同一平面创造约束条件。视图的垂直平面只能约束在另一个垂直平面上(左右<—>左右,上下<—>上下);基准线只能约束到其他基准线上。

调整视图尺寸:(Attributes 窗口

一。大小设置

  • xx dp(Fixed):特定尺寸

  • Wrap Content:默认大小,视图仅在需要时扩展。应用此属性后,如果大小超过了某一侧的约束(app:layout_constraintxxx_toxxxOf="xxx")则以本身大小为主,约束可能会失效。如果希望强制使用约束来设置大小的话,可以使用app:layout_constrainedWidth=”true|false”app:layout_constrainedHeight=”true|false”

  • Match Constraints:使用0dp (MATCH_CONSTRAINT)+app:layout_constraintxxx_toxxxOf="xxx"会尽可能根据设定好的相对位置来占用整个空间,以满足每侧的约束条件。一旦这样设置大小就是固定的。

    一般应用这个属性用layout_constraintWidth_min和layout_constraintHeight_min等属性可设置最大最小尺寸。

二。设置百分比和宽高比

百分比:

 app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.1"

宽高比:

 <Button android:layout_width="wrap_content"
                   android:layout_height="0dp"      <!--需要宽高中至少一个为0dp-->
                   app:layout_constraintDimensionRatio="1:1" />   <!--值可以是宽:高,也可以是小数(宽和高的比值)-->

如果宽高均为0dp,会满足比值的同时根据约束来最大限度地填充,可以通过在比值前添加W或H来表示约束那一方。比如 app:layout_constraintDimensionRatio="H,1:1" 表示高跟随宽的变化而变化,比率是1:1.

调整视图位置

一。相对位置

  • layout_constraintLeft_toLeftOf

  • layout_constraintLeft_toRightOf

  • layout_constraintRight_toLeftOf

  • layout_constraintRight_toRightOf

  • layout_constraintTop_toTopOf

  • layout_constraintTop_toBottomOf

  • layout_constraintBottom_toTopOf

  • layout_constraintBottom_toBottomOf

  • layout_constraintBaseline_toBaselineOf(baseline 是文字基准线,可用于两个控件中的文字进行对齐)

  • layout_constraintStart_toEndOf

  • layout_constraintStart_toStartOf

  • layout_constraintEnd_toStartOf

  • layout_constraintEnd_toEndOf

  1. 一般添加了相对两侧的约束后控件会在该维度居中,可通过app:layout_constraintHorizontal_bias="xxx"和app:layout_constraintVertical_bias="xxx"调整权重以控制偏向谁(xxx:0-1小数)

2. 想要固定控件一侧的位置以及几个控件的相对位置可以用guidline

<android.support.constraint.Guideline
            android:id="@+id/guideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"<!--垂直的guidline,也可设置水平horizontal-->
            app:layout_constraintGuide_end="35dp" /><!--相对父容器的位置,也可设置相对父容器的percent-->

其他的控件根据guideline位置设置约束 app:layout_constraintxxx_toxxxOf="@id/guideline"

二。环形位置

以一个控件的中心为圆心,在另一个控件中设定圆的半径和角度即可确定另一个控件的位置

 

<Button android:id="@+id/buttonA" ... />
  <Button android:id="@+id/buttonB" ...
      app:layout_constraintCircle="@+id/buttonA"
      app:layout_constraintCircleRadius="100dp"
      app:layout_constraintCircleAngle="45" />

当有控件gone时

可以通过如下属性设置gone后的边距

  • layout_goneMarginStart

  • layout_goneMarginEnd

  • layout_goneMarginLeft

  • layout_goneMarginTop

  • layout_goneMarginRight

  • layout_goneMarginBottom

三。链:双向位置约束

  • Spread:视图是均匀分布的(在考虑外边距之后)。这是默认值。

  • Spread inside:第一个和最后一个视图固定在链两端的约束边界上,其余视图均匀分布。

  • Weighted:默认“match constraints”会将空间均分,但可以使用 layout_constraintHorizontal_weight 和 layout_constraintVertical_weight 属性为每个视图分配重要性权重。(同LinearLayout中 layout_weight的作用)

  • Packed:视图打包在一起(在考虑外边距之后)。 然后,您可以通过更改链的头视图偏差调整整条链的偏差(左/右或上/下)。

四。Barrier屏障

barrierDirection 指定方向,constraint_referenced_ids引用的控件 id(多个id以逗号隔开)。

app:constraint_referenced_ids="TextView1,TextView2" />

注意:使用时要进行互相约束(指的是Barrier左右或者上下两边的控件都要对barrier进行约束才有效)。

五。组Group

可以把多个控件归为一组,方便隐藏或显示一组控件

app:constraint_referenced_ids="TextView3,TextView4" />

P.S.

*Autoconnect 为当前view自动添加相应的相对父view的约束

*Infer Constraints自动创建全局的约束条件

*ConstraintSet 可为元素的尺寸和位置添加动画效果

RelativeLayout

指定子视图相对于父视图或彼此(由 ID 确定)的位置。默认情况下,所有子视图均绘制在布局的左上角,使用各种不同的属性定义其位置(居中,对齐左边等)

LinearLayout

*android:orientation属性指定布局方向。

*android:layout_weight 设置布局权重,例如等分:

  • android:layout_height="0dp"(针对垂直布局,android:layout_width ="0dp"针对水平布局)。

  • android:layout_weight ="1"

FrameLayout

圈出整个屏幕的某块区域以显示某个view。通常用来保存单个view,因为很难在不重叠的情况下根据屏幕大小进行大小变换。但可以添加多个子view并通过android:layout_gravity控制位置

*子view存在栈里(最后添加的最先显示),frameLayout尺寸为最大子view尺寸(不在乎是否显示)。View.GONE仅当setConsiderGoneChildrenWhenMeasuring() 设置为true时,才可调整视图大小。

TableLayout

布局指定子view的确切位置(xy坐标)

*子级不能指定宽度(layout_width),必须是match_parent,但高度(layout_height)可以,默认是wrap_content

*该表的列与单元格最多的行一样多。

*TableLayout的典型子级是TableRow,但实际上可以将任何View子类用作TableLayout的直接子级。视图将显示为跨越所有列的单行。

收缩列:setColumnShrinkable(int index,boolean isShirnkable)

拉伸列:setColumnStretchable(int index,boolean isStrechable)

隐藏列:setColumnCollapsed(int index,boolean isCollaspable)

添加子view:addView(View child,int index,ViewGroup.LayoutParams params

AbsoluteLayout

布局指定子view的确切位置(xy坐标)

布局灵活性差,在API Level3中被弃用

androidx.gridlayout

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值