【发布时间】:2021-10-06 13:08:33
【问题描述】:
我想在我的 Android ConstraintLayout 中添加 3 个按钮,以便它们水平跨越。我认为这可以通过使用链来完成。所以我用它们制作了一条链,然后扩展了它们的视图(通过标记它们 --> 右键单击 --> 组织 --> 水平扩展)。然而,这只是将它们扩展到一个特定的设备。切换到另一台设备时,按钮看起来不再正确。这是我的代码:
<?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:id="@+id/outerConstraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/rigthInnerConstraintLayout"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/white"
app:layout_constraintBottom_toBottomOf="@+id/outerConstraintLayout"
app:layout_constraintEnd_toEndOf="@+id/outerConstraintLayout"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/outerConstraintLayout"
app:layout_constraintTop_toTopOf="@+id/outerConstraintLayout"
app:layout_constraintVertical_bias="1.0">
<Button
android:id="@+id/button_1"
android:layout_width="425dp"
android:layout_height="@dimen/_30sdp"
android:text="Button 1"
app:layout_constraintEnd_toStartOf="@+id/button_2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button_2"
android:layout_width="425dp"
android:layout_height="@dimen/_30sdp"
android:text="Button 2"
app:layout_constraintEnd_toStartOf="@+id/button_3"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button_1"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button_3"
android:layout_width="426dp"
android:layout_height="@dimen/_30sdp"
android:text="Button_3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button_2"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
对于我使用可缩放的高度,我使用可缩放的尺寸单位 (https://github.com/intuit/sdp),但对于按钮的宽度,dp 值由 Android Studio 分配(在展开之后)。我认为这就是问题所在。这些 dp 值是硬编码的。这意味着在更改设备时,值仍然相同,这会导致外观不正确。我的问题是如何将 3 个按钮排列成一个链(或其他),以便它们水平跨越任何设备的一整行?
【问题讨论】:
标签: android android-layout android-constraintlayout