【问题标题】:Strange TextView behaviour in ConstraintLayoutConstraintLayout 中奇怪的 TextView 行为
【发布时间】:2017-03-06 18:53:45
【问题描述】:

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

标题概览和回收站视图之间的差距非常小。但是我从来没有定义任何边距,当传记变长时,这个边距甚至会变得更大


这不是自定义 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 没有重复,因为这个问题是关于间距行为,另一个问题是关于为什么长文本视图隐藏在另一个视图后面而短文本文本视图没有

【问题讨论】:

  • 检查您是否有错误的边距或填充值。
  • 不,我没有,抱歉我忘了添加 xml 文件。现在添加了
  • 您需要逐个添加项目以布局检查哪一个导致此问题。仅通过阅读 xml 上的代码很难理解问题。
  • 我试过了,导致这个bug的是movie_overview_value

标签: java android xml android-layout textview


【解决方案1】:

您可以尝试使用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>

【讨论】:

  • 谢谢,但我能够使用 relativelayout 创建一个工作布局,但是自从发布了稳定的 constraintlayout 我想做同样的事情,但使用约束布局
【解决方案2】:

我认为这是因为您刚刚为 textview 的最顶部边缘提供了锚点,尝试从顶部和底部相对于上下视图设置锚点。

【讨论】:

  • 不确定您的意思是什么。能不能解释的详细一点?
  • 当你在设计模式下打开你的布局然后选择文本视图然后你会在该文本视图周围看到一个矩形,在矩形的每个边缘的中心你会看到一个圆圈,只需将它拖到在同一平面上查看上视图并对下边缘执行相同操作,它将相对于上视图和下视图分配 textview 的权重。
  • 现在视图完全不可见
  • 尝试在此页面上观看视频:- developer.android.com/training/constraint-layout/index.html
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-07
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多