约束布局ConstraintLayout按钮堆叠问题解决

约束布局ConstraintLayout按钮堆叠问题解决

在学《第一行代码(第二版)》中,书上用的是LinearLayout,但是android给的默认布局是ConstraintLayout,在敲书2.2.4的代码时,遇到按钮堆叠问题。

问题描述:
先是按书上敲的如下代码

在这里插入图片描述

结果Button爆红,按以前的做法是用Infer Constraints(如下图)
在这里插入图片描述代码就变成了下图
红框内为自动添加代码

然后就变成了两个按钮重叠(如下图)
在这里插入图片描述

按照Android的ConstraintLayout布局控件堆积一起的解决方法在第二个按钮中添加了

app:layout_constraintTop_toBottomOf="@id/start_normal_activity"

依旧重叠。

解决方法

删除layout_constraintBaseline_toBaselineOf

app:layout_constraintBaseline_toBaselineOf="@+id/start_normal_activity"

在这里插入图片描述
问题解决:

在这里插入图片描述

问题原因:

layout_constraintBaseline_toBaselineOf是设定两个控件的基线对齐,什么是基线对齐?可以参考下图

这里B的与A的基线对齐

而上面的第二个按钮不仅基线对齐,还是宽高都和第一个按钮的一样,于是就重叠了。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.5" /></androidx.constraintlayout.widget.ConstraintLayout> ### 回答2: 在约束布局中,可以使用以下XML代码来实现在中心位置放置按钮的效果: ```xml <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/centerButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="中心按钮" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> ``` 在此约束布局中,我们使用了一个ConstraintLayout作为根布局,并在中心放置了一个Button按钮。通过设置按钮约束条件,使其分别位于父布局的上下左右边界,从而实现在中心位置放置按钮的效果。 其中,`app:layout_constraintStart_toStartOf="parent"`表示按钮的起始边与父控件的起始边对齐;`app:layout_constraintEnd_toEndOf="parent"`表示按钮的结束边与父控件的结束边对齐;`app:layout_constraintTop_toTopOf="parent"`表示按钮的顶部边与父控件的顶部边对齐;`app:layout_constraintBottom_toBottomOf="parent"`表示按钮的底部边与父控件的底部边对齐。 通过以上设置按钮将会处于父布局的中心位置。 ### 回答3: Android中使用约束布局ConstraintLayout)来实现中心位置放置按钮的方式如下: ```xml <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/btn_center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="中心按钮" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> ``` 在约束布局中,我们将按钮(Button)设置为中心位置,并使用`app:layout_constraintTop_toTopOf="parent"`和`app:layout_constraintBottom_toBottomOf="parent"`属性使按钮的上下边界与父布局的上下边界对齐,使用`app:layout_constraintStart_toStartOf="parent"`和`app:layout_constraintEnd_toEndOf="parent"`属性使按钮的左右边界与父布局的左右边界对齐,从而实现按钮在中心的效果。 以上代码中,`android:layout_width="wrap_content"`和`android:layout_height="wrap_content"`属性用于设置按钮的宽度和高度为包裹内容。 使用约束布局可以更灵活地布局控件,还可以通过约束布局的其他属性来进一步定制控件的位置和约束关系。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值