如何根据约束布局中的内容创建视图?
How to create a view based on its content in constraint layout?
我有一个视图需要显示在底部,并且视图高度应基于内容(视图的 link 与父级一起编辑)。如果我 link 父级 error_view
的顶部约束,则使用其整个 space。
constraintLayout
里面的constraintLayout
有什么用,什么时候去?此解决方案是否适合我的问题?
<androidx.constraintlayout.widget.ConstraintLayout ...>
<View
android:id="@+id/error_view"
android:layout_width="0dp"
android:layout_height="160dp" // How to keep height based on content rather than defined height??
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="40dp"
android:background="@drawable/display_result"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/textureView"
/>
<TextView
android:id="@+id/error_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="12dp"
android:fontFamily="@font/poppins_medium"
android:text="@string/error_msg"
android:textAlignment="center"
android:textColor="@color/white"
app:layout_constraintBottom_toTopOf="@+id/error_try_again"
app:layout_constraintEnd_toEndOf="@+id/error_view"
app:layout_constraintStart_toStartOf="@+id/error_view"
app:layout_constraintTop_toTopOf="@+id/error_view" />
<Button
android:id="@+id/error_try_again"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="24dp"
android:background="@drawable/primary_button"
android:text="@string/try_again"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/error_view"
app:layout_constraintEnd_toEndOf="@+id/error_view"
app:layout_constraintStart_toStartOf="@+id/error_view" />
</androidx.constraintlayout.widget.ConstraintLayout>
您需要使用嵌套约束布局,或者您可以根据需要使用任何其他 ViewGroup,如 RelativeLayout、LinearLayout 等。
在你的情况下,当你查看高度应该根据内容,所以你不能固定它的高度。
建议的解决方案是:
<androidx.constraintlayout.widget.ConstraintLayout ...>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="40dp"
android:background="@drawable/display_result">
<TextView
android:id="@+id/error_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="12dp"
android:fontFamily="@font/poppins_medium"
android:text="@string/error_msg"
android:textAlignment="center"
android:textColor="@color/white"
app:layout_constraintBottom_toTopOf="@+id/error_try_again"
app:layout_constraintEnd_toEndOf="@+id/error_view"
app:layout_constraintStart_toStartOf="@+id/error_view"
app:layout_constraintTop_toTopOf="@+id/error_view" />
<Button
android:id="@+id/error_try_again"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="24dp"
android:background="@drawable/primary_button"
android:text="@string/try_again"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/error_view"
app:layout_constraintEnd_toEndOf="@+id/error_view"
app:layout_constraintStart_toStartOf="@+id/error_view" />
</constraint>
</androidx.constraintlayout.widget.ConstraintLayout>
我有一个视图需要显示在底部,并且视图高度应基于内容(视图的 link 与父级一起编辑)。如果我 link 父级 error_view
的顶部约束,则使用其整个 space。
constraintLayout
里面的constraintLayout
有什么用,什么时候去?此解决方案是否适合我的问题?
<androidx.constraintlayout.widget.ConstraintLayout ...>
<View
android:id="@+id/error_view"
android:layout_width="0dp"
android:layout_height="160dp" // How to keep height based on content rather than defined height??
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="40dp"
android:background="@drawable/display_result"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/textureView"
/>
<TextView
android:id="@+id/error_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="12dp"
android:fontFamily="@font/poppins_medium"
android:text="@string/error_msg"
android:textAlignment="center"
android:textColor="@color/white"
app:layout_constraintBottom_toTopOf="@+id/error_try_again"
app:layout_constraintEnd_toEndOf="@+id/error_view"
app:layout_constraintStart_toStartOf="@+id/error_view"
app:layout_constraintTop_toTopOf="@+id/error_view" />
<Button
android:id="@+id/error_try_again"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="24dp"
android:background="@drawable/primary_button"
android:text="@string/try_again"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/error_view"
app:layout_constraintEnd_toEndOf="@+id/error_view"
app:layout_constraintStart_toStartOf="@+id/error_view" />
</androidx.constraintlayout.widget.ConstraintLayout>
您需要使用嵌套约束布局,或者您可以根据需要使用任何其他 ViewGroup,如 RelativeLayout、LinearLayout 等。
在你的情况下,当你查看高度应该根据内容,所以你不能固定它的高度。 建议的解决方案是:
<androidx.constraintlayout.widget.ConstraintLayout ...>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="40dp"
android:background="@drawable/display_result">
<TextView
android:id="@+id/error_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="12dp"
android:fontFamily="@font/poppins_medium"
android:text="@string/error_msg"
android:textAlignment="center"
android:textColor="@color/white"
app:layout_constraintBottom_toTopOf="@+id/error_try_again"
app:layout_constraintEnd_toEndOf="@+id/error_view"
app:layout_constraintStart_toStartOf="@+id/error_view"
app:layout_constraintTop_toTopOf="@+id/error_view" />
<Button
android:id="@+id/error_try_again"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="24dp"
android:background="@drawable/primary_button"
android:text="@string/try_again"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/error_view"
app:layout_constraintEnd_toEndOf="@+id/error_view"
app:layout_constraintStart_toStartOf="@+id/error_view" />
</constraint>
</androidx.constraintlayout.widget.ConstraintLayout>