java - ConstraintLayout 中奇怪的 TextView 行为

标签 java android xml android-layout textview

我有一个应用程序中 Actor 的详细页面布局。它包含一个信息框,其中包含最重要的信息、传记和 Actor 正在播放的电影的回收器 View 。但是,当传记很短时,它看起来是这样的:

Short Biography

标题概述和回收器 View 之间的距离非常小。然而,我从未定义过任何边距,当传记变长时,这个边距甚至会变得更大

<小时/>

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因为这个问题是关于间距行为,另一个问题是关于为什么长 TextView 隐藏在另一个 View 后面,而短文本 TextView 则不然

最佳答案

您可以尝试使用LinearLayout来定位 View ,将此代码放在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>

关于java - ConstraintLayout 中奇怪的 TextView 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42633366/

相关文章:

java - 在 Netbeans GUI 框架中使用按钮

java - 使用 IMAP(javamail API)从 gmail 访问电子邮件

android - 安卓版 Lombok

java - Libgdx 和服务器游戏

c# - 以编程方式创建 xml 文件

java - 从 Activity B 返回到 Activity A 的 fragment

java - DST 与 Joda Time 的转换

android - 如何使用 gradlew 列出所有构建变体?

java - javax StreamSource 的目的是什么

ruby - 类型错误:无法将 Builder::XmlMarkup 转换为数组