ConstraintLayout中的奇怪TextView行为

时间:2017-03-06 18:53:46

标签: java android xml android-layout textview

我有一个appin的详细页面布局。它包含一个信息框,其中包含最重要的信息,传记和回收者视图以及演员正在播放的电影。但是当传记很短时,它看起来就像这样:

Short Biography

标题概述与回收者视图之间的边距非常小。然而,我从未定义任何保证金,当传记变得更长时,这个边际甚至变得更大

enter image description here

enter image description here

这不是自定义TextView的错误,因为使用普通的TextView也会发生同样的奇怪行为。有人知道如何解决这种奇怪的行为吗?

这是xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/detail_holder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.design.widget.AppBarLayout
                android:id="@+id/detail_app_bar_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

                <com.mt.moviesiwanttowatch.CustomToolbar
                    android:id="@+id/drawer_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin" />
            </android.support.design.widget.AppBarLayout>

            <android.support.constraint.ConstraintLayout
                android:id="@+id/movie_overview_holder"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="?attr/actionBarSize"
                android:background="?attr/detail_card_2_color"
                android:paddingLeft="@dimen/material_layout_keylines_horizontal_mobile_margin"
                android:paddingRight="@dimen/material_layout_keylines_horizontal_mobile_margin"
                android:paddingTop="@dimen/material_layout_keylines_horizontal_mobile_margin">


                <android.support.v7.widget.CardView
                    android:id="@+id/info_holder"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="16dp"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintBottom_toTopOf="@+id/overview"
                    >

                    <RelativeLayout
                        android:id="@+id/basic_info_container"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="?attr/detail_card_1_color"
                        android:orientation="horizontal"
                        android:paddingBottom="@dimen/material_layout_keylines_horizontal_mobile_margin"
                        android:paddingLeft="56dp"
                        android:paddingRight="24dp">

                        <!-- Poster Area -->
                        <FrameLayout
                            android:id="@+id/poster_image_container"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"
                            android:layout_centerVertical="true">

                            <!-- Default Image -->
                            <ImageView
                                android:id="@+id/poster_image_default"
                                android:layout_width="0dp"
                                android:layout_height="0dp"
                                android:scaleType="centerCrop"
                                android:src="@drawable/default_poster"
                                android:visibility="gone"
                                tools:ignore="contentDescription" />

                            <com.mt.moviesiwanttowatch.ui.widget.ThemeIcon
                                android:id="@+id/poster_image_def"
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:adjustViewBounds="true"
                                android:scaleType="centerCrop"
                                android:src="@drawable/ic_add_alarm_white_48dp"
                                android:visibility="gone"
                                custom:iconColor="?attr/icon_color"
                                tools:ignore="contentDescription" />

                            <android.support.v7.widget.CardView
                                android:id="@+id/poster_image_holder"
                                android:layout_width="@dimen/detail_poster_width"
                                android:layout_height="@dimen/detail_poster_height"
                                android:layout_alignParentBottom="true"
                                android:layout_marginLeft="@dimen/material_layout_keylines_horizontal_mobile_margin"
                                android:layout_marginStart="@dimen/material_layout_keylines_horizontal_mobile_margin">

                                <com.android.volley.toolbox.NetworkImageView
                                    android:id="@+id/poster_image"
                                    android:layout_width="@dimen/detail_poster_width"
                                    android:layout_height="@dimen/detail_poster_height"
                                    android:layout_alignParentBottom="true"
                                    android:background="@color/default_image_background"
                                    android:padding="1dp"
                                    android:scaleType="centerCrop"

                                    />
                            </android.support.v7.widget.CardView>

                            <!-- Poster Image -->


                        </FrameLayout>

                        <!-- Text Area -->
                        <LinearLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentLeft="true"
                            android:layout_toLeftOf="@id/poster_image_container"
                            android:gravity="center_vertical"
                            android:orientation="vertical">

                            <!-- Title -->
                            <com.mt.moviesiwanttowatch.ui.widget.TextViewRoboto
                                android:id="@+id/cast_name"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="@dimen/material_layout_keylines_horizontal_mobile_margin"
                                android:lines="1"
                                android:textSize="@dimen/text_size_subheading"
                                custom:robotoType="bold"
                                tools:text="01 January 2016\n120 mins" />

                            <com.mt.moviesiwanttowatch.ui.widget.TextViewRoboto
                                android:id="@+id/cast_birthdy"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="@dimen/material_layout_vertical_spacing_between_content_areas"
                                android:lines="1"

                                android:textSize="@dimen/text_size_subheading"
                                custom:robotoType="bold"
                                tools:text="01 January 2016\n120 mins" />


                            <com.mt.moviesiwanttowatch.ui.widget.TextViewRoboto
                                android:id="@+id/cast_age"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="@dimen/material_layout_vertical_spacing_between_content_areas"
                                android:lines="1"

                                android:textSize="@dimen/text_size_subheading"
                                custom:robotoType="bold"
                                tools:text="01 January 2016\n120 mins" />

                            <com.mt.moviesiwanttowatch.ui.widget.TextViewRoboto
                                android:id="@+id/cast_homepage"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="@dimen/material_layout_vertical_spacing_between_content_areas"
                                android:lines="1"
                                android:textSize="@dimen/text_size_subheading"
                                custom:robotoType="bold"
                                tools:text="01 January 2016\n120 mins" />

                        </LinearLayout>
                    </RelativeLayout>
                </android.support.v7.widget.CardView>

                <com.mt.moviesiwanttowatch.ui.widget.ThemeIcon
                    android:id="@+id/overview_icon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:adjustViewBounds="true"
                    android:src="@drawable/ic_info_white_24dp"
                    app:iconColor="@color/accent"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintTop_toTopOf="@+id/overview"
                    app:layout_constraintBottom_toBottomOf="@+id/overview"/>

                <com.mt.moviesiwanttowatch.ui.widget.TextViewRoboto
                    android:id="@+id/overview"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="56dp"
                    android:layout_marginTop="16dp"
                    android:text="@string/detail_header_overview"
                    android:textSize="16sp"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/info_holder"
                    app:layout_constraintBottom_toTopOf="@+id/movie_overview_value"
                    custom:robotoType="bold" />


                <!-- Value -->
                <com.mt.moviesiwanttowatch.ui.widget.TextViewRoboto
                    android:id="@+id/movie_overview_value"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:textSize="@dimen/text_size_body2"
                    app:layout_constraintLeft_toLeftOf="@+id/overview"
                    app:layout_constraintRight_toRightOf="@id/info_holder"
                    app:layout_constraintTop_toBottomOf="@+id/overview_icon"
                    app:layout_constraintBottom_toTopOf="@+id/sidescroll"
                    custom:robotoType="regular"
                    />

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/sidescroll"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    app:layoutManager="android.support.v7.widget.LinearLayoutManager"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/movie_overview_value"
                    />


            </android.support.constraint.ConstraintLayout>
        </android.support.design.widget.Coortextview-goes-behind-other-view-with-long-texts-constraintlayoutdinatorLayout>

    </android.support.v4.widget.NestedScrollView>
</layout>

编辑:这不是这个问题https://stackoverflow.com/questions/42613994/textview-goes-behind-other-view-with-long-texts-constraintlayout的重复,因为这个问题是关于间距行为的,另一个问题是关于为什么长文本视图隐藏在另一个视图后面而短文本视图不存在

3 个答案:

答案 0 :(得分:0)

您可以尝试使用LinearLayout定位视图,将此代码放在CardView下方

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

     <com.mt.moviesiwanttowatch.ui.widget.ThemeIcon
                android:id="@+id/overview_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:src="@drawable/ic_info_white_24dp"
                app:iconColor="@color/accent"
                />

            <com.mt.moviesiwanttowatch.ui.widget.TextViewRoboto
                android:id="@+id/overview"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="56dp"
                android:layout_marginTop="16dp"
                android:text="@string/detail_header_overview"
                android:textSize="16sp"
                custom:robotoType="bold" />


            <!-- Value -->
            <com.mt.moviesiwanttowatch.ui.widget.TextViewRoboto
                android:id="@+id/movie_overview_value"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:textSize="@dimen/text_size_body2"
                custom:robotoType="regular"
                />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/sidescroll"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                />

</LinearLayout>

答案 1 :(得分:0)

我认为这是因为您只是为textview的最顶端提供锚点,尝试从上部和下部视图的顶部和底部设置锚点。

答案 2 :(得分:0)

最新的约束布局版本不存在此错误。