我想一个布局,具有以下总体布局层次ConstraintLayout> CardView> ConstraintLayout>按钮。
第二届constraintLayout必须坚持cardView的右下角。
但卡内查看约束不工作。
我已经第一次尝试与LinearLayout中更换第二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的右下角被贴。实际的结果是:
设置你的内心ConstraintLayout是这样的:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1 "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
你可以不适用强制你cardview孩子,适用android:layout_gravity="bottom|right"
您ConstraintLayout
内CardView
你必须让你的内部布局qazxsw POI像下面,然后就可以轻松实现你qazxsw POI。
match_parent
试试这个
constraints
你cardview的内部约束布局应具有宽度和高度match_parent。否则约束布局将缩小,以匹配其子的宽度和高度。
<?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"
tools:context=".home_fragment"
android:layout_height="match_parent"
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="match_parent"
android:layout_height="match_parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Button 1 "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="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="match_parent"
android:layout_height="match_parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Button 2 "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.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="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Button 1 "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="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="match_parent"
android:layout_height="match_parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Button 2 "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.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 = "match_parent" // change this line
android:layout_height = "match_parent" // change this line
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_constraintRight_toRightOf = "parent"
app:layout_constraintBottom_toBottomOf = "parent"
android:layout_marginBottom = "8dp" />
</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 = "match_parent" // change this line
android:layout_height = "match_parent" // change this line
app:layout_constraintRight_toRightOf = "parent"
app:layout_constraintBottom_toBottomOf = "parent"
>
<Button
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
app:layout_constraintRight_toRightOf = "parent"
app:layout_constraintBottom_toBottomOf = "parent"
android:text = "Button 2 "
/>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
试试这个代码上设置约束按钮右边的按钮:
截图:<?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:id="@+id/home_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".home_fragment">
<android.support.v7.widget.CardView
android:id="@+id/first_card"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_marginTop="200dp"
app:cardBackgroundColor="@color/colorAccent"
app:cardCornerRadius="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Button 1 "
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="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/colorAccent"
app:cardCornerRadius="20dp"
app:layout_constraintStart_toStartOf="@id/first_card"
app:layout_constraintTop_toBottomOf="@id/first_card">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2 "
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
出色地实现您预期的结果,则不需要使用CardView内部约束布局:
你可以做,没有利用内幕CardView约束如下。
就在 <android.support.v7.widget.CardView
android:id="@+id/first_card"
android:layout_width="200dp"
android:layout_height="100dp"
app:cardBackgroundColor="#3F51B5"
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="match_parent"
android:layout_height="match_parent"
android:id="@+id/button_1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1 "
app:layout_constraintEnd_toEndOf="@id/button_1"
app:layout_constraintBottom_toBottomOf="@+id/button_1" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
适用于您的按钮CardView内
检查下面的更新的代码和截图:
android:layout_gravity="bottom|end"