ConstraintLayout布局 居中|居右 实现。

ConstraintLayout布局出来之后,一直没怎么使用过,今天写一个布局文件刚好使用了一下。

界面结构是这样的:


这里主要是左边的视图有两部分,一个ImageView控件和LinearLayout组成,LinearLayout控件中又有三个TextView,如果左边的根布局使用LinearLayout的话感觉会很繁琐,而ConstraintLayout控件的强大之处除了其拖拽的作用之外,还在于各种控件居中的方便性,以及多控件同时布局的时候可以加载在同一根布局下面,而不需要进行嵌套布局的使用。(同时,ConstraintLayout的优势是Google对它进行了大量的优化,在加载的时候性能要高于其他布局。)


先放一下写好的布局效果图:



具体实现中主要是用到:


 	app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toTopOf="parent"
        app:layout_constraintTop_toBottomOf="parent"
 这样就可以保持让它居中和靠右。


下载源码

(android命令行截屏命令: screencap 路径+名字.png)

### ConstraintLayout实现视图底部居中布局方式 在 `ConstraintLayout` 中,可以通过设置约束条件使某个视图相对于父容器或其他兄弟视图实现底部居中效果。具体来说,要让一个视图在其父容器的底部区域内水平居中并垂直对齐到指定位置,可以使用以下属性组合: #### 属性说明 - **`app:layout_constraintBottom_toBottomOf="parent"`**: 将当前视图的底部与父容器的底部对齐[^1]。 - **`app:layout_constraintStart_toStartOf="parent"` 和 `app:layout_constraintEnd_toEndOf="parent"`**: 让视图的起始边和结束边分别与父容器的起始边和结束边对齐,从而实现水平方向上的居中[^2]。 如果需要进一步调整视图的位置比例(例如距离底部一定百分比),还可以结合 `Guideline` 或者 `Bias` 来完成更复杂的定位需求。 以下是具体的 XML 示例代码展示如何在一个 `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/bottomCenteredButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bottom Centered Button" app:layout_constraintBottom_toBottomOf="parent" <!-- 设置到底部边缘 --> app:layout_constraintStart_toStartOf="parent" <!-- 左侧对齐至父级起点 --> app:layout_constraintEnd_toEndOf="parent" <!-- 右侧对齐至父级终点 --> /> </androidx.constraintlayout.widget.ConstraintLayout> ``` 通过以上配置即可轻松达成目标——即按钮会自动放置于整个界面最底端正中央处显示出来[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值