如何将视图宽度设置为父宽度的百分比 - ConstraintLayout 中的边距为 space?
How to set view width to be a percent of parent width - some space for margins in ConstraintLayout?
我想创建如下图所示的布局:
所以每个按钮占(父宽度 - space 边距)的 50%
如果我将 layout_constraintWidth_percent
设置为 0.5,则它不起作用。我可以使用 LinearLayout
和重量来实现,但是如何使用 ConstraintLayout
?
这是我使用的布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<Button
android:id="@+id/other_options"
style="?tertiaryButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/half_pad"
android:text="@string/report_suicide_other_options_action"
app:layout_constraintWidth_percent="0.5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
<Button
android:id="@+id/yes"
style="?primaryButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/half_pad"
android:text="@string/action_yes"
app:layout_constraintWidth_percent="0.5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/other_options"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
只需添加:
app:layout_constraintEnd_toStartOf="@+id/yes"
到 ID 为 other_options
的 Button
您可以删除
app:layout_constraintWidth_percent="0.5"
来自两个按钮。
我想创建如下图所示的布局:
所以每个按钮占(父宽度 - space 边距)的 50%
如果我将 layout_constraintWidth_percent
设置为 0.5,则它不起作用。我可以使用 LinearLayout
和重量来实现,但是如何使用 ConstraintLayout
?
这是我使用的布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<Button
android:id="@+id/other_options"
style="?tertiaryButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/half_pad"
android:text="@string/report_suicide_other_options_action"
app:layout_constraintWidth_percent="0.5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
<Button
android:id="@+id/yes"
style="?primaryButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/half_pad"
android:text="@string/action_yes"
app:layout_constraintWidth_percent="0.5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/other_options"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
只需添加:
app:layout_constraintEnd_toStartOf="@+id/yes"
到 ID 为 other_options
Button
您可以删除
app:layout_constraintWidth_percent="0.5"
来自两个按钮。