在Android开发中,线性布局(LinearLayout)是一种常用的布局方式,它按照水平或垂直方向排列子视图。要将按钮与屏幕底部对齐,需要理解布局权重(weight)和重力(gravity)的概念。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 这个View会占据所有剩余空间 -->
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<!-- 按钮会被推到屏幕底部 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="底部按钮"/>
</LinearLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="底部按钮"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
<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: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"/>
</androidx.constraintlayout.widget.ConstraintLayout>
为什么我的按钮没有对齐到底部? 可能原因:
如何让按钮水平居中同时位于底部? 在ConstraintLayout中同时设置:
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
在RelativeLayout中设置:
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
没有搜到相关的文章