我正在尝试将SwipeRefreshLayout
包含子项ListView
的内容设置为.DialogFragment
实际上使Dialog包装它的孩子的高度,因为即使ListView只有一项,它也总是填充窗口高度。
布局 :
<?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>
结果 :
我需要根据ListView项目使DialogFragment窗口缩放高度,而不总是填充窗口高度。
通过制作 theSwipeRefreshLayout
或 theListView
来包装内容(项目)。
我已经尝试了很多方法来做到这一点,但没有任何效果。
我怎样才能做到这一点?
更新 :
它SwipeRefreshLayout
本身正在填充整个窗口高度并忽略wrap_content
.
我替换ListView
为包装内容高度RecyclerView
,RecyclerView
这很好。但仍然SwipeRefreshLayout
是填充高度,迫使DialogFragment覆盖所有Window。
我将对话框高度设置为wrap_content
:
@Override
public void onResume() {
getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
super.onResume();
}
这是list_item.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">
<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>
更新 2:
使用RecyclerView
和包装它,LinearLayout
这样wrap_content就可以了。
<?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>
但仍然SwipeRefreshLayout
是在填补高度。