问题描述
我试图将包含子项SwipeRefreshLayout
的{{1}}设置为ListView
中的 wrap_content 。
实际上是使对话框包裹其子级的高度,因为即使 ListView 仅具有一项。
布局:
DialogFragment
结果:
我需要根据 ListView 项来设置 DialogFragment 窗口缩放高度,而不是始终填充窗口高度。
通过制作<?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">
<RelativeLayout
android:id="@+id/footer"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/devider_line"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/myButton" />
</RelativeLayout>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:layout_above="@id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</RelativeLayout>
或SwipeRefreshLayout
来包装内容(项目)。
我尝试了很多方法来做,但是没有用。
我该如何实现?
更新:
ListView
本身正在填充整个窗口高度,而忽略了SwipeRefreshLayout
。
我用wrap_content
取代了ListView
,而RecyclerView
包裹了内容高度,这很好。但RecyclerView
仍然是填充高度,迫使 DialogFragment 覆盖所有 Window 。
我将对话框的高度设置为SwipeRefreshLayout
:
wrap_content
这是 list_item.xml:
@Override
public void onResume() {
getDialog().getwindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT);
super.onResume();
}
更新2:
使用<?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">
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="18dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:gravity="start"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:text="@string/default_text" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:gravity="end"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/default_text"
android:textAlignment="center" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/container"
android:background="@color/devider_color" />
</RelativeLayout>
并用RecyclerView
包装,以便 wrap_content 可以使用它。
LinearLayout
但是<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ffff">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff00ff" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/myButton" />
</LinearLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
仍在填充高度。
解决方法
ListView
的行为类似,请尝试使用此子类覆盖此行为
public class WrapingHeightListView extends ListView {
public WrapingHeightListView(Context context) {
super(context);
}
public WrapingHeightListView(Context context,AttributeSet attrs) {
super(context,attrs);
}
public WrapingHeightListView(Context context,AttributeSet attrs,int defStyle) {
super(context,attrs,defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec,int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec,expandSpec);
}
}
在XML中使用<com.yourpackage.WrapingHeightListView...
代替当前的<ListView ...
标记
或者您可以切换到RecyclerView
,它可以正确处理wrap_content
的高度(但这还需要一些小的适配器重构)