【问题标题】:Textview invisible when used together with fragment inside ConstraintLayout与 ConstraintLayout 中的片段一起使用时,Textview 不可见
【发布时间】:2018-09-03 21:16:34
【问题描述】:

我使用 YouTubePlayerFragment 作为上面带有 textview 的片段。不知何故,标题被隐藏了,渲染时只有空白。很明显,TextView 存在那里,但没有显示。当我将可见性设置为不可见时,我得到了同样的效果。

现在:

目标:

查看此视图​​的 XML:

<?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">

    <TextView
        android:id="@+id/tv_title"
        style="@style/movieTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/details_title_left"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="Trailers"
        android:layout_marginLeft="@dimen/details_title_left" />

    <fragment
        android:id="@+id/youtubeFragment"
        android:name="com.google.android.youtube.player.YouTubePlayerFragment"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_title" />



</android.support.constraint.ConstraintLayout>

在主要活动中,我将此布局与包含标签一起使用,并在单击按钮时更改其可见性。

【问题讨论】:

  • 您在TextView 上设置了android:visibility="invisible",所以我希望它是不可见的...
  • 这只是一个例子。没有它就不会出现。截图后忘记删除了。问题已编辑。
  • 您确定@style/movieTitle 中的文本颜色和背景颜色不同吗?
  • 是的。在另一个包含的布局中使用了相同的样式,该布局显示标题为“关于”的概述。
  • 如何设置TextView中显示的文字?确定不为空?

标签: android android-fragments youtube youtube-data-api android-constraintlayout


【解决方案1】:

片段在约束布局中是臭名昭著的。将 Fragment 放入 FrameLayout 并对其进行约束。另外我建议给 FrameLayout 一个固定的高度或使用height="0dp" 的比例。

<FrameLayout
        android:id="@+id/frameContainer"
        android:layout_width="0dp"
        android:layout_height="120dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_title">
        <fragment
                android:id="@+id/youtubeFragment"
                android:name="com.google.android.youtube.player.YouTubePlayerFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
</FrameLayout>

编码愉快:D

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-19
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多