我正在实现一个
DialogFragment
,里面有很多内容,我可以滚动该DialogFragment
,所以我正在使用:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/area_frg"
android:orientation="vertical"
tools:context=".MtPopupDialogFragmentPage">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.AREATracerManager.AppBarOverlay">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/topDialogAppBar"
style="@style/Widget.MaterialComponents.Toolbar.Primary"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:navigationIcon="?attr/homeAsUpIndicator"
app:subtitle="Back to main"
app:subtitleTextColor="@color/white" />
</com.google.android.material.appbar.AppBarLayout>
.
.
.
.
.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/btnResetFW2"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_margin="8dp"
android:layout_gravity="center|center_horizontal"
android:contentDescription="@string/app_name"
android:text="@string/st_restart"
android:textColor="@color/area_bck"
android:textSize="17dp"
android:textAllCaps="true"
android:padding="8dp"
app:layout_anchorGravity="center|center_horizontal"/>
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/btnCheckSW2"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_margin="8dp"
android:layout_gravity="center|center_horizontal"
android:contentDescription="@string/app_name"
android:text="@string/st_check"
android:textColor="@color/bck"
android:textSize="17dp"
android:textAllCaps="true"
android:padding="8dp"
app:layout_anchorGravity="center|center_horizontal"/>
</LinearLayout>
</LinearLayout>
但是最后的内容缩小到屏幕的高度,但我想滚动并查看所有内容...
我还尝试用
ConstraintLayout
而不是 LinearLayout
更改顶部节点,但它没有解决我的目标...也许这不可能用 DialogFragment
实现?
当然可以!为了确保 DialogFragment 的内容可以正确滚动和显示,您应该按照以下步骤操作:
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Place your content layout components here -->
</ScrollView>
onCreateDialog
在WindowManager.LayoutParams
方法中调整DialogFragment的高度。@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
// Set dialog height to a percentage of screen height
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int dialogHeight = (int) (displayMetrics.heightPixels * 0.8); // Adjust as needed
params.height = dialogHeight;
dialog.getWindow().setAttributes(params);
return dialog;
}
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/lastFab"
app:layout_constraintBottom_toBottomOf="parent"
<!-- Other attributes here -->
/>
如果您按照这些步骤操作,它应该可以正常工作。希望有帮助