如何在Android Java中使DialogFragment弹出窗口高于屏幕?

问题描述 投票:0回答:1

我正在实现一个

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
实现?

java android-dialogfragment
1个回答
-1
投票

当然可以!为了确保 DialogFragment 的内容可以正确滚动和显示,您应该按照以下步骤操作:

  1. ScrollView 设置:将整个内容布局包裹在 ScrollView 中。这使得用户可以在内容超出屏幕高度时滚动浏览内容。
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <!-- Place your content layout components here -->
</ScrollView>
  1. 对话框高度调整:为了防止任何内容被裁剪,请使用
    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;
}
  1. ConstraintLayout 便利性:如果您对 ConstraintLayout 感到满意,那么它适合构建您的内容。只需确保 ScrollView 中的最后一个项目锚定到底部即可。
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
    android:id="@+id/lastFab"
    app:layout_constraintBottom_toBottomOf="parent"
    <!-- Other attributes here -->
/>

如果您按照这些步骤操作,它应该可以正常工作。希望有帮助

© www.soinside.com 2019 - 2024. All rights reserved.