使用 Android ConstraintLayout 2.0.0 将项目放入一行可以通过以下流程完成:
build.gradle
文件中添加 ConstraintLayout 2.0.0 依赖。可以通过以下方式添加:implementation 'androidx.constraintlayout:constraintlayout:2.0.0'
<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/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
app:layout_constraintStart_toEndOf="@+id/button1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
在上述布局中,Button 1
使用了 app:layout_constraintEnd_toStartOf="@+id/button2"
将其右边界与 Button 2
的左边界相连。同时,Button 2
使用了 app:layout_constraintStart_toEndOf="@+id/button1"
将其左边界与 Button 1
的右边界相连。这样,两个按钮就会水平排列在一行。
希望这些信息能够对你有所帮助!如果有其他问题,请随时提问。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云