1 回答
TA贡献1111条经验 获得超0个赞
ConstraintLayout的主要优点是允许您使用平面视图层次结构创建大型且复杂的布局。没有像RelativeLayout或LinearLayout等那样的嵌套视图组。您可以使用ConstraintLayout为android制作响应式UI,与RelativeLayout和GridLayout相比,它更灵活。
ConstraintLayout 允许您轻松地将任何视图限制为percentage width or height
. 所以我稍微编辑了你的代码。兄弟看看吧,希望对你有帮助。
<?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"
android:orientation="horizontal"
android:padding="16dp">
<LinearLayout
android:id="@+id/a1"
android:layout_width="0dp"
app:layout_constraintWidth_percent=".48"
android:layout_height="0dp"
app:layout_constraintHeight_percent=".3"
android:background="@drawable/blacksquare"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:padding="8dp"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="@+id/a2"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent=".48"
app:layout_constraintHeight_percent=".3"
android:background="@drawable/blacksquare"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:padding="8dp"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="@+id/a3"
android:layout_width="0dp"
app:layout_constraintWidth_percent=".48"
android:layout_height="0dp"
app:layout_constraintHeight_percent=".3"
android:background="@drawable/blacksquare"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/a1"
android:layout_marginTop="16dp"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="@+id/a4"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent=".48"
app:layout_constraintHeight_percent=".3"
android:background="@drawable/blacksquare"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@id/a2"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="@+id/a5"
android:layout_width="0dp"
app:layout_constraintWidth_percent=".48"
android:layout_height="0dp"
app:layout_constraintHeight_percent=".3"
android:background="@drawable/blacksquare"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/a3"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="@+id/a6"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent=".48"
app:layout_constraintHeight_percent=".3"
android:background="@drawable/blacksquare"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@id/a4"
android:orientation="horizontal">
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
添加回答
举报