【发布时间】:2019-06-28 21:38:32
【问题描述】:
我正在尝试具有以下一般布局层次结构的布局 ConstraintLayout>CardView>ConstraintLayout>Button.
第二个约束布局必须贴在cardView的右下角。
但卡片视图中的约束不起作用。
我首先尝试用 LinearLayout 替换 2nd ConstraintLayout,但它也没有帮助。约束对它们没有影响。
<?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:context=".home_fragment"
android:id="@+id/home_fragment">
<android.support.v7.widget.CardView
android:id="@+id/first_card"
android:layout_width="200dp"
android:layout_height="100dp"
app:cardBackgroundColor="@color/card_color"
app:cardCornerRadius="15dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="200dp"
>
<android.support.constraint.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1 "
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/second_card"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_marginTop="56dp"
app:cardBackgroundColor="@color/card_color"
app:cardCornerRadius="20dp"
app:layout_constraintStart_toStartOf="@id/first_card"
app:layout_constraintTop_toBottomOf="@id/first_card">
<android.support.constraint.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2 "
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
但是这里第二个 ConstraintLayout 被粘在 CardView 的左上角。 我希望它贴在 CardView 的右下角。 实际结果:
【问题讨论】:
-
用你的按钮使用 app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" 就可以了
-
约束布局不是这样工作的
-
约束布局用于移除多个父布局,您应该只在 1 个布局中使用 1 个约束布局,只要您不强制添加它(例如滚动视图需要一个子布局)。移除卡片视图内的约束布局,直接将按钮保留在主约束内,并将它们约束到卡片视图
-
@DeepPatel 没有帮助。将高度和宽度更改为“match_parent”有效。
-
@Redman Sir 直接在 CardView 中放置按钮也不接受约束。一开始我也是这么做的。但是没有用。
标签: android android-studio android-button android-cardview android-constraintlayout