【发布时间】:2021-01-26 10:26:51
【问题描述】:
我正在尝试在DialogFragment 中将包含子ListView 的SwipeRefreshLayout 设置为wrap_content。
真正让 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 窗口缩放高度,而不是总是填充窗口高度。
通过使用SwipeRefreshLayout 或ListView 来包装内容(项目)。
我已经尝试了很多方法来做到这一点,但都没有奏效。
我怎样才能做到这一点?
更新:
SwipeRefreshLayout 本身填充了整个窗口高度,而忽略了wrap_content。
我用RecyclerView 替换了ListView 并且RecyclerView 是包装内容高度,这很好。但 SwipeRefreshLayout 仍然是 filling height 强制 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 和 Wrapping 它与 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 仍在填充高度。
【问题讨论】:
标签: android listview swiperefreshlayout dialogfragment android-wrap-content