【发布时间】:2016-07-14 17:14:08
【问题描述】:
我遇到了一个我无法解决的问题。我有一个带有CardView 的片段,其中包含RecyclerView。这个RecyclerView 有layout_height="wrap_content" 但我可以看到这不起作用。我在这里读过一些solutions 说在支持库的版本 23.2 中这是固定的,但在我的情况下它不起作用。另外,我想要一个TextView 就在CardView 的正下方,但它没有显示出来。如果我给CardView 一个固定的高度,它工作正常。
我希望有人可以帮助解决这个问题。
编辑 让我们看看这张图片是否能更好地解释我的意思。
这里,RecyclerView 限制是蓝色矩形,而它们应该类似于红色矩形,这就是我想要归档的。
build.gradle
dependencies{
compile 'com.android.support:recyclerview-v7:24.+'
}
layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:padding="5dp"
card_view:cardElevation="4dp">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.v7.widget.CardView>
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="test text"
android:textSize="20sp"/>
【问题讨论】:
-
所以你想让你的布局同时包含recyclerview和一个文本视图?
-
@Eenvincible 很好,就是这样。但主要问题是
ReciclerViewwrap_content 不起作用。 textview 问题可以通过我相信重量的布局来解决,但它不会在下面。 -
有 wrap_content 为 RecyclerView 当你有足够的项目来填满可见布局时,肯定会隐藏下面的文本视图;这就是为什么设置高度会使文本视图可见。让我尝试从我所拥有的答案
-
是的,你是对的@eenvincible,但是当recyclerview中只有一个元素时也会发生这种情况,所以真正的问题是recyclerview获取了所有可用空间,所以它没有包装内容.. .
标签: android android-recyclerview