Top_toBottomof 在 constraintlayout 中不起作用。如何在约束布局中使用Top_toBottomof?
Top_toBottomof is not working in constraintlayout . How to use Top_toBottomof in Constraint layout?
我有以下布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<include layout="@layout/toolbar_with_filter_back" />
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/banner"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="0dp" />
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="165dp" />
<android.support.v7.widget.CardView
android:id="@+id/win_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/_5sdp"
android:elevation="@dimen/_8sdp"
app:cardCornerRadius="@dimen/_10sdp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
app:layout_constraintTop_toBottomOf="@+id/guideline">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="@dimen/_10sdp">
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:gravity="center"
android:scaleType="fitXY"
app:cardCornerRadius="8dp"
app:cardPreventCornerOverlap="true">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/exec_business_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:scaleType="fitXY"
android:src="@drawable/icon_dairy" />
<TextView
android:id="@+id/exec_business_iconname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/exec_card_second_title"
android:fontFamily="@font/boutrosasma_regular"
android:gravity="center"
android:text="Foods"
android:textColor="@color/white"
android:textSize="@dimen/text_size_h" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center">
<TextView
android:id="@+id/tvRname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:ellipsize="end"
android:fontFamily="@font/boutrosasma_bold"
android:gravity="center"
android:text="@string/exe_size"
android:textColor="@color/exec_card_second_title"
android:textSize="@dimen/text_size_h_valve"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/fb_sr_bu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:ellipsize="end"
android:fontFamily="@font/boutrosasma_regular"
android:gravity="center"
android:text="SR"
android:textColor="@color/exec_card_size_val_color"
android:textSize="@dimen/text_size_h"
android:textStyle="bold" />
<TextView
android:id="@+id/fb_size_val_bu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="@dimen/_5sdp"
android:ellipsize="end"
android:fontFamily="@font/boutrosasma_bold"
android:gravity="center"
android:text="56.9"
android:textColor="@color/exec_card_size_val_color"
android:textSize="@dimen/_14ssp"
android:textStyle="bold" />
<TextView
android:id="@+id/fb_bn_bu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:ellipsize="end"
android:fontFamily="@font/boutrosasma_regular"
android:gravity="center"
android:text="BN"
android:layout_marginLeft="@dimen/_5sdp"
android:textColor="@color/exec_card_size_val_color"
android:textSize="@dimen/text_size_h"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/layer_5" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center">
<TextView
android:id="@+id/tvgrowth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:ellipsize="end"
android:fontFamily="@font/boutrosasma_bold"
android:gravity="center"
android:text="@string/exe_growth"
android:textColor="@color/exec_card_second_title"
android:textSize="@dimen/text_size_h_valve"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="@+id/fb_growth_bu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:ellipsize="end"
android:fontFamily="@font/boutrosasma_bold"
android:gravity="center"
android:text="+7.0%"
android:textColor="@color/exec_growth_val_color"
android:textSize="@dimen/_14ssp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/card_topplayer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/_5sdp"
android:elevation="@dimen/_8sdp"
app:cardCornerRadius="@dimen/_10sdp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
app:layout_constraintTop_toBottomOf="@+id/win_card"
app:layout_constraintEnd_toEndOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/top_player_back"
android:padding="@dimen/_10sdp">
<TextView
android:id="@+id/top_player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/boutrosasma_bold"
android:text="@string/business_unit_top"
android:textColor="@color/exec_card_size_val_color" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:src="@drawable/orientation" />
</RelativeLayout>
<com.github.mikephil.charting.charts.LineChart
android:layout_margin="@dimen/_5sdp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/business_lineChart"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
问题 1:
当我将第二个 cardview 高度设置为 match_parent 时,它会占据全屏而不是底部的左屏幕。这意味着它也将覆盖第一个 cardview 并到达 parent/screen 的顶部。那么在这种情况下,为什么设置约束 toptobottomof 不起作用,我怎样才能让它起作用?据我所知,toptobottomof 确保您的第二个视图将始终保持在第一个视图的下方,在这种情况下这是行不通的。
问题二:
如果我保持布局不变,那么问题是折线图根本没有扩展。它将整个图表高度压缩到包装内容高度中。它的行为不像它需要更多 space,因为这是 wrap_content 属性的目的,而是将图表本身限制在可用高度并且根本不扩展 cardview 高度。
请帮我解决这些问题。
将高度更改为 android.support.v7.widget.CardView 的 0dp
,将宽度更改为 0dp
并使用 app:layout_constraintBottom_toBottomOf="parent"
这将使高度填充到父
添加这些
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
示例
<androidx.cardview.widget.CardView
android:id="@+id/card_topplayer"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="@dimen/_5sdp"
android:elevation="@dimen/_8sdp"
app:cardCornerRadius="@dimen/_10sdp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/win_card">
更新
要实现滚动视图,请使用 fillviewport = true
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:fillViewport = "true"
android:layout_height="0dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
然后像这样
从第二个cardview
中删除这个app:layout_constraintEnd_toEndOf="parent"
<android.support.v7.widget.CardView
android:id="@+id/card_topplayer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/_5sdp"
android:elevation="@dimen/_8sdp"
app:cardCornerRadius="@dimen/_10sdp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
app:layout_constraintTop_toBottomOf="@+id/win_card"
>
我有以下布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<include layout="@layout/toolbar_with_filter_back" />
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/banner"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="0dp" />
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="165dp" />
<android.support.v7.widget.CardView
android:id="@+id/win_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/_5sdp"
android:elevation="@dimen/_8sdp"
app:cardCornerRadius="@dimen/_10sdp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
app:layout_constraintTop_toBottomOf="@+id/guideline">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="@dimen/_10sdp">
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:gravity="center"
android:scaleType="fitXY"
app:cardCornerRadius="8dp"
app:cardPreventCornerOverlap="true">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/exec_business_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:scaleType="fitXY"
android:src="@drawable/icon_dairy" />
<TextView
android:id="@+id/exec_business_iconname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/exec_card_second_title"
android:fontFamily="@font/boutrosasma_regular"
android:gravity="center"
android:text="Foods"
android:textColor="@color/white"
android:textSize="@dimen/text_size_h" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center">
<TextView
android:id="@+id/tvRname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:ellipsize="end"
android:fontFamily="@font/boutrosasma_bold"
android:gravity="center"
android:text="@string/exe_size"
android:textColor="@color/exec_card_second_title"
android:textSize="@dimen/text_size_h_valve"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/fb_sr_bu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:ellipsize="end"
android:fontFamily="@font/boutrosasma_regular"
android:gravity="center"
android:text="SR"
android:textColor="@color/exec_card_size_val_color"
android:textSize="@dimen/text_size_h"
android:textStyle="bold" />
<TextView
android:id="@+id/fb_size_val_bu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="@dimen/_5sdp"
android:ellipsize="end"
android:fontFamily="@font/boutrosasma_bold"
android:gravity="center"
android:text="56.9"
android:textColor="@color/exec_card_size_val_color"
android:textSize="@dimen/_14ssp"
android:textStyle="bold" />
<TextView
android:id="@+id/fb_bn_bu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:ellipsize="end"
android:fontFamily="@font/boutrosasma_regular"
android:gravity="center"
android:text="BN"
android:layout_marginLeft="@dimen/_5sdp"
android:textColor="@color/exec_card_size_val_color"
android:textSize="@dimen/text_size_h"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/layer_5" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center">
<TextView
android:id="@+id/tvgrowth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:ellipsize="end"
android:fontFamily="@font/boutrosasma_bold"
android:gravity="center"
android:text="@string/exe_growth"
android:textColor="@color/exec_card_second_title"
android:textSize="@dimen/text_size_h_valve"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="@+id/fb_growth_bu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:ellipsize="end"
android:fontFamily="@font/boutrosasma_bold"
android:gravity="center"
android:text="+7.0%"
android:textColor="@color/exec_growth_val_color"
android:textSize="@dimen/_14ssp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/card_topplayer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/_5sdp"
android:elevation="@dimen/_8sdp"
app:cardCornerRadius="@dimen/_10sdp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
app:layout_constraintTop_toBottomOf="@+id/win_card"
app:layout_constraintEnd_toEndOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/top_player_back"
android:padding="@dimen/_10sdp">
<TextView
android:id="@+id/top_player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/boutrosasma_bold"
android:text="@string/business_unit_top"
android:textColor="@color/exec_card_size_val_color" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:src="@drawable/orientation" />
</RelativeLayout>
<com.github.mikephil.charting.charts.LineChart
android:layout_margin="@dimen/_5sdp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/business_lineChart"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
问题 1: 当我将第二个 cardview 高度设置为 match_parent 时,它会占据全屏而不是底部的左屏幕。这意味着它也将覆盖第一个 cardview 并到达 parent/screen 的顶部。那么在这种情况下,为什么设置约束 toptobottomof 不起作用,我怎样才能让它起作用?据我所知,toptobottomof 确保您的第二个视图将始终保持在第一个视图的下方,在这种情况下这是行不通的。
问题二: 如果我保持布局不变,那么问题是折线图根本没有扩展。它将整个图表高度压缩到包装内容高度中。它的行为不像它需要更多 space,因为这是 wrap_content 属性的目的,而是将图表本身限制在可用高度并且根本不扩展 cardview 高度。
请帮我解决这些问题。
将高度更改为 android.support.v7.widget.CardView 的 0dp
,将宽度更改为 0dp
并使用 app:layout_constraintBottom_toBottomOf="parent"
这将使高度填充到父
添加这些
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
示例
<androidx.cardview.widget.CardView
android:id="@+id/card_topplayer"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="@dimen/_5sdp"
android:elevation="@dimen/_8sdp"
app:cardCornerRadius="@dimen/_10sdp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/win_card">
更新 要实现滚动视图,请使用 fillviewport = true
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:fillViewport = "true"
android:layout_height="0dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
然后像这样
从第二个cardview
中删除这个app:layout_constraintEnd_toEndOf="parent"
<android.support.v7.widget.CardView
android:id="@+id/card_topplayer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/_5sdp"
android:elevation="@dimen/_8sdp"
app:cardCornerRadius="@dimen/_10sdp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
app:layout_constraintTop_toBottomOf="@+id/win_card"
>