【问题标题】:How to keep views from disappearing in wrap_content如何防止视图在 wrap_content 中消失
【发布时间】:2016-09-02 10:44:38
【问题描述】:

当我的视图布局为 wrap_content 时,我的视图在我的 RecyclerView 中消失了。 RecyclerView 是 match_parent 两个方向,并使用一个 LinearLayout 来扩展下面的 xml 文件。以下是 RecyclerView 的每一部分的外观:

这里是它的 XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp">

    <View
        android:id="@+id/scheduleBlockColor"
        android:layout_width="15dp"
        android:layout_height="match_parent"
        android:layout_alignBottom="@+id/scheduleBlockText"
        android:background="@color/colorCougarRed" />

    <RelativeLayout
        android:id="@+id/scheduleBlockText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@id/scheduleBlockColor"
        android:layout_toRightOf="@id/scheduleBlockColor"
        android:paddingEnd="0dp"
        android:paddingLeft="5dp"
        android:paddingRight="0dp"
        android:paddingStart="5dp">

        <TextView
            android:id="@+id/scheduleBlockTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:text="@string/schedule_block_time_default"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/scheduleBlockClassName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/scheduleBlockTime"
            android:ellipsize="end"
            android:padding="2dp"
            android:singleLine="true"
            android:text="@string/schedule_block_class_default"
            android:textColor="#000000"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/scheduleBlockRoom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/scheduleBlockClassName"
            android:padding="2dp"
            android:text="@string/schedule_block_room_default"
            android:textSize="16sp" />
    </RelativeLayout>

</RelativeLayout>

我的问题是,当 RecyclerView 加载时,如何防止彩色部分消失?如果我将上面的父 RelativeLayout 设置为 match_parent,它可以正常工作。我试过this answer,但没有同样的成功

【问题讨论】:

  • 可以给整个RecyclerView截图吗?
  • 也发布有问题的屏幕截图
  • @Code-Apprentice 这是颜色失败的屏幕截图。 i.imgur.com/APjn11a.png?1
  • 所有的文字肯定仍然像有色块一样,我觉得很奇怪。

标签: java android


【解决方案1】:

这是因为&lt;View&gt; 上设置的高度。它因为没有内容而变得一团糟(即使是match_parent)。由于意图是有一个垂直条纹,您可以将其锚定到父级的顶部和底部。你已经在为底部做这件事(有点,将它与文本视图容器的底部对齐),所以你只需要处理顶部锚定:

<View
    android:id="@+id/scheduleBlockColor"
    ...
    android:layout_alignParentTop="true"
    ... />

【讨论】:

  • 谢谢,就是这样。我从没想过不定义顶部会很重要,尤其是因为它在工作室中工作。
【解决方案2】:

而不是在您的 RelativeLayout 中使用 layout_margin 进行填充。

【讨论】:

  • 这个解决方案不走运,假设您指的是 XML 顶部的 RelativeLayout
猜你喜欢
  • 2018-06-01
  • 2016-07-15
  • 2017-03-20
  • 2022-01-12
  • 1970-01-01
  • 2012-08-07
  • 1970-01-01
  • 2011-04-14
  • 2015-11-12
相关资源
最近更新 更多