Android 为什么链元素之间的空间如此之大?
我需要将一行三个按钮与背景视图对齐,如下所示: 为此,我创建了一个背景视图(Android 为什么链元素之间的空间如此之大?,android,android-constraintlayout,Android,Android Constraintlayout,我需要将一行三个按钮与背景视图对齐,如下所示: 为此,我创建了一个背景视图(@drawable/background)和一个链(@id/button1,@id/button2,@id/button3) 活动\u main.xml: <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.co
@drawable/background
)和一个链(@id/button1
,@id/button2
,@id/button3
)
活动\u main.xml:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/background"
android:layout_width="0dp"
android:layout_height="32dp"
android:layout_marginEnd="48dp"
android:layout_marginStart="48dp"
android:layout_marginTop="16dp"
android:background="@drawable/background"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="24dp"
android:layout_marginStart="4dp"
android:background="@drawable/button_background"
android:gravity="center"
android:text="Button1"
app:layout_constraintBottom_toBottomOf="@id/background"
app:layout_constraintEnd_toStartOf="@id/button2"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="@id/background"
app:layout_constraintTop_toTopOf="@id/background" />
<TextView
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="24dp"
android:background="@drawable/button_background"
android:gravity="center"
android:text="Button2"
app:layout_constraintBottom_toBottomOf="@id/background"
app:layout_constraintEnd_toStartOf="@id/button3"
app:layout_constraintStart_toEndOf="@id/button1"
app:layout_constraintTop_toTopOf="@id/background" />
<TextView
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="24dp"
android:layout_marginEnd="4dp"
android:background="@drawable/button_background"
android:gravity="center"
android:text="Button3"
app:layout_constraintBottom_toBottomOf="@id/background"
app:layout_constraintEnd_toEndOf="@id/background"
app:layout_constraintStart_toEndOf="@id/button2"
app:layout_constraintTop_toTopOf="@id/background" />
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="16dp" />
<solid android:color="#11000000" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="12dp" />
<stroke android:width="2dp" android:color="@color/colorPrimary" />
<solid android:color="#ffffff" />
</shape>
. 在第一个视图之前和最后一个视图之后应该有4dp的边距,按钮之间没有空格
更新
我试过constraint layout的1.0.2版和所有beta版,我只能在constraint layout:1.1.0-beta5
中重现这个问题。这是否意味着在这个版本的constraint layout中有一个bug,或者我使用它的方式是错误的
更新2
这实际上是beta 5版中的一个bug,也是beta 6版中的一个bug。我不知道为什么会发生这种情况,也许这确实是1.1.0-beta5版中的一个bug,但是
应用于背景
视图的开始/结束边距由三个按钮组成的链继承,因为该链被约束到背景
的开始和结束。如果删除这些边距,“间隙”会突然消失:
<View
android:id="@+id/background"
android:layout_width="0dp"
android:layout_height="32dp"
android:layout_marginTop="16dp"
android:background="@drawable/background"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
您的代码在我的系统中运行良好device@MohammadAli这并不重要,任何都可以。你的代码在我的像素模拟器上运行得很好。尝试使用其他设备。还要确保在build.gradle中为constraint layou属性使用了正确的min sdk版本。我忘了提到我正在使用constraint layou的beta版本,因为我的项目中需要障碍。这可能是问题吗?@fdermishin可能是因为它与com.android.support.constraint:constraint layout:1.0.2配合得很好。
谢谢,这种变通方法对我很有效。然而,奇怪的是,背景的边距会影响按钮的边距。我已经把这件事告诉了谷歌,我希望他们能在下一个版本中改变这种行为。现在,我将使用beta4版本,因为它可以按预期工作。
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/background"
android:layout_width="0dp"
android:layout_height="32dp"
android:layout_marginTop="16dp"
android:layout_marginStart="48dp"
android:layout_marginEnd="48dp"
android:background="@drawable/background"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<TextView
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="24dp"
android:layout_marginStart="52dp"
android:gravity="center"
android:text="Button1"
android:background="@drawable/button_background"
app:layout_constraintTop_toTopOf="@id/background"
app:layout_constraintBottom_toBottomOf="@id/background"
app:layout_constraintEnd_toStartOf="@id/button2"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"/>
<TextView
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="24dp"
android:gravity="center"
android:text="Button2"
android:background="@drawable/button_background"
app:layout_constraintTop_toTopOf="@id/background"
app:layout_constraintBottom_toBottomOf="@id/background"
app:layout_constraintEnd_toStartOf="@id/button3"
app:layout_constraintStart_toEndOf="@id/button1"/>
<TextView
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="24dp"
android:layout_marginEnd="52dp"
android:gravity="center"
android:text="Button3"
android:background="@drawable/button_background"
app:layout_constraintTop_toTopOf="@id/background"
app:layout_constraintBottom_toBottomOf="@id/background"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/button2"/>
</android.support.constraint.ConstraintLayout>