
永远位于底部居中的按钮 1.使用FrameLayout <FrameLayout xmlns:andro android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|bottom" android:text="底部居中按钮" />FrameLayout>
2.使用LinearLayout <LinearLayout xmlns:andro android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <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:layout_gravity="center_horizontal" android:text="底部居中按钮" />LinearLayout>
3.又是使用LinearLayout <LinearLayout xmlns:andro android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="bottom|center_horizontal" android:orientation="vertical"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="底部居中按钮" />LinearLayout>
4.使用RelativeLayout <RelativeLayout xmlns:andro android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="底部居中按钮" />RelativeLayout>
5.使用ConstraintLayout布局 <androidx.constraintlayout.widget.ConstraintLayout xmlns:andro android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"> <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent" android:text="底部居中按钮" />androidx.constraintlayout.widget.ConstraintLayout>
6.compose版实现 Scaffold(bottomBar = { Box( modifier = Modifier.fillMaxWidth(), contentAlignment = Alignment.Center ) { Button(onClick = { /*TODO*/ }) { Text(text = "底部居中按钮") } } }) { }
7.又一个compose版本实现 Column( modifier = Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally ) { Box(modifier = Modifier.weight(1f)) Button(onClick = { /*TODO*/ }) { Text(text = "底部居中按钮") } }
8.还是一个compose版本实现 ConstraintLayout(modifier = Modifier.fillMaxSize()) { val btn = createRef() Button( modifier = Modifier.constrainAs(btn) { start.linkTo(parent.start) end.linkTo(parent.end) bottom.linkTo(parent.bottom) }, onClick = { /*TODO*/ }) { Text(text = "底部居中按钮") } }
|