在ConstraintLayout中创建链的正确方法是什么,其中每个链链接都有多个小部件?
我希望链条为:(简单按钮和分数)===(中等按钮和分数)===(硬按钮和分数)。
显然,传统的做法是将每个按钮和按钮放在一起。 textview对进入自己的布局,并链接那些布局。但我的理解是ConstraintLayouts应该避免这种情况:布局中没有布局,一切都被夷为平地。可以这样做,还是“平面布局”规则不可行?
注意:这是简化的。我的活动实际上在每个组中有更多的组件,所以我正在寻找一个通用的解决方案,而不是特定于每个2个组件的链接。
我的XML(TextViews完全不受约束):
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="25dp">
<Button
android:id="@+id/main_menu_difficulty_hard"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:paddingEnd="50sp"
android:paddingStart="50sp"
android:text="@string/hard"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/main_menu_difficulty_medium"
app:layout_constraintVertical_chainStyle="spread_inside"/>
<Button
android:id="@+id/main_menu_difficulty_medium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingEnd="50sp"
android:paddingStart="50sp"
android:text="@string/medium"
app:layout_constraintBottom_toTopOf="@+id/main_menu_difficulty_hard"
app:layout_constraintTop_toBottomOf="@+id/main_menu_difficulty_easy"
app:layout_constraintVertical_chainStyle="spread_inside"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.502"/>
<Button
android:id="@+id/main_menu_difficulty_easy"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="50dp"
android:paddingEnd="50sp"
android:paddingStart="50sp"
android:text="@string/easy"
app:layout_constraintBottom_toTopOf="@+id/main_menu_difficulty_medium"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="spread_inside"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Best score: 20"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="328dp"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Best score: 20"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="558dp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Best score: 20"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="98dp"/>
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:1)
嗯,这会有所帮助:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="25dp">
<Button
android:id="@+id/main_menu_difficulty_hard"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:paddingEnd="50sp"
android:paddingStart="50sp"
android:text="@string/hard"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/main_menu_difficulty_medium"
app:layout_constraintVertical_chainStyle="spread_inside" />
<Button
android:id="@+id/main_menu_difficulty_medium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:paddingEnd="50sp"
android:paddingStart="50sp"
android:text="@string/medium"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="spread_inside" />
<Button
android:id="@+id/main_menu_difficulty_easy"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:paddingEnd="50sp"
android:paddingStart="50sp"
android:text="@string/easy"
app:layout_constraintBottom_toTopOf="@+id/main_menu_difficulty_medium"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="spread_inside" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Best score: 20"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.028"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/main_menu_difficulty_medium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Best score: 20"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.028"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/main_menu_difficulty_hard" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="16dp"
android:text="Best score: 20"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/main_menu_difficulty_easy" />
</android.support.constraint.ConstraintLayout>