Android RecyclerView不尊重高度=包装内容?
我有一个聊天屏幕,它包含两个视图:一个用于显示先前聊天的回收器视图,一个用于封装编辑文本的自定义视图,一个发送按钮,另一个用于让用户选择其他要发送的内容(如照片等)的回收器视图。此自定义视图称为面板 因此,我的聊天活动具有以下布局:Android RecyclerView不尊重高度=包装内容?,android,android-support-library,android-recyclerview,Android,Android Support Library,Android Recyclerview,我有一个聊天屏幕,它包含两个视图:一个用于显示先前聊天的回收器视图,一个用于封装编辑文本的自定义视图,一个发送按钮,另一个用于让用户选择其他要发送的内容(如照片等)的回收器视图。此自定义视图称为面板 因此,我的聊天活动具有以下布局: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:orientation="vertical" android:layout_width="match_pa
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<chat.example.app.widget.Panel
style="@style/PanelStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:id="@+id/panel" />
<android.support.v7.widget.RecyclerView
android:background="@android:color/holo_red_dark"
android:layout_margin="@dimen/activity_horizontal_margin"
android:scrollbars="vertical"
android:id="@+id/previous_chat"
android:layout_above="@id/panel"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
<?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:background="@android:color/white">
<RelativeLayout
android:id="@+id/staging_area_root"
android:layout_gravity="center"
android:gravity="center"
android:paddingLeft="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Chat box and send -->
</RelativeLayout>
<!-- THIS GUY IS PROBLEMATIC -->
<android.support.v7.widget.RecyclerView
android:id="@+id/card_set"
android:layout_margin="15dp"
android:layout_below="@id/staging_area_root"
android:background="@android:color/holo_blue_bright"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
浏览最新的android开发者博客:
此版本为LayoutManager API带来了一个激动人心的新功能:
自动测量!这允许RecyclerView根据
内容的大小。这意味着以前不可用
场景,例如将换行内容用于
回收视图,现在是可能的。你会发现所有内置的
LayoutManager现在支持自动测量
我错过什么了吗
更新1:
我在Mr.Problem recycler视图中添加了一个适配器,其中包含0项。仍然没有解决办法
更新2:
截图:
代码片段取自23.2 RecyclerViewOnMeasure调用:
mLayout.setMeasuredDimensionsFromChildren调用将根据RecyclerView的子对象的大小进行自动测量。也就是说,触发自动测量需要一个包含1个或多个子项的有效RecyclerView.适配器。不要在面板上使用android:layout\u gravity,而是使用android:layout\u alignParentBottom和android:layout\u CenterHorizone。我复制粘贴了你的布局,删除了面板xml并替换为它的布局xml,在Android Studio的预览中一切看起来都很好。@AlexTownsend不走运。作为更新,我正在使用alignParentBottom。还没有更新这里的代码。也许这与我的自定义视图的创建方式有关?你可以在Android Studio中发布预览屏幕截图,或者从设备/模拟器发布屏幕截图吗?@AlexTownsend你想要哪个屏幕截图?你让我更改的属性@阿列克斯汤森完成了。蓝色回收视图来自面板。无法发送+1按钮的垃圾邮件。非常感谢
compile 'com.android.support:recyclerview-v7:23.2.0'
if (mLayout.mAutoMeasure) {
final int widthMode = MeasureSpec.getMode(widthSpec);
final int heightMode = MeasureSpec.getMode(heightSpec);
final boolean skipMeasure = widthMode == MeasureSpec.EXACTLY
&& heightMode == MeasureSpec.EXACTLY;
mLayout.onMeasure(mRecycler, mState, widthSpec, heightSpec);
if (skipMeasure || mAdapter == null) {
return;
}
if (mState.mLayoutStep == State.STEP_START) {
dispatchLayoutStep1();
}
// set dimensions in 2nd step. Pre-layout should happen with old dimensions for
// consistency
mLayout.setMeasureSpecs(widthSpec, heightSpec);
mState.mIsMeasuring = true;
dispatchLayoutStep2();
// now we can get the width and height from the children.
mLayout.setMeasuredDimensionFromChildren(widthSpec, heightSpec);
// if RecyclerView has non-exact width and height and if there is at least one child
// which also has non-exact width & height, we have to re-measure.
if (mLayout.shouldMeasureTwice()) {
mLayout.setMeasureSpecs(
MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
mState.mIsMeasuring = true;
dispatchLayoutStep2();
// now we can get the width and height from the children.
mLayout.setMeasuredDimensionFromChildren(widthSpec, heightSpec);
}
}