java - NestedScrollView 未显示在 AlertDialog 中

标签 java android

我有一个 AlertDialog,它有一个自定义 View ,以下是 View :

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    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">

    <androidx.appcompat.widget.Toolbar
        android:background="@color/grey_1"
        android:id="@+id/edit_point_toolbar"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/edit_scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <ImageView
                android:id="@+id/edit_point_close"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:src="@drawable/ic_close_red_32dp"
                android:layout_margin="4dp"
                android:clickable="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <TextView
                app:layout_constraintRight_toLeftOf="@id/edit_point_close"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:text="Edit point"
                android:textColor="@color/black"
                android:textSize="15dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.appcompat.widget.Toolbar>

    <androidx.core.widget.NestedScrollView
        android:id="@+id/edit_scrollView"
        app:layout_constraintTop_toBottomOf="@id/edit_point_toolbar"
        android:layout_width="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_height="0dp">
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="8dp">
            <EditText
                android:id="@+id/edit_point_name"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintBottom_toTopOf="@id/save_edit_point"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:minLines="3"
                android:textDirection="rtl"
                android:background="@drawable/new_book_text_box"
                android:gravity="right|top"
                android:textAlignment="gravity"
                android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
            <Button
                app:layout_constraintTop_toBottomOf="@id/edit_point_name"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                android:id="@+id/save_edit_point"
                android:layout_width="0dp"
                app:layout_constraintWidth_percent="0.7"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="8dp"
                android:backgroundTint="@color/green_500"
                android:text="Confirm"
                android:textColor="#FFF" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

在 xml 设计屏幕中,我可以正常看到工具栏和 NestedScrollView 内容,但在我的应用程序中,NestedScrollView 没有显示,我只能看到工具栏。

这是AlertDialog代码:

 AlertDialog.Builder builder = new AlertDialog.Builder(context , R.style.Theme_Dialog);
                ViewGroup viewGroup = view.findViewById(android.R.id.content);
                View dialogView = LayoutInflater.from(view.getContext()).inflate(R.layout.edit_point_dialog , viewGroup, false);
                builder.setView(dialogView);
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
                Window window = alertDialog.getWindow();
                window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

我很困惑为什么 NestedScrollView 内容没有显示。事实上,当我设置高度 wrap_content 而不是 0dp 时,它会显示,但滚动效果不佳并且与工具栏重叠。 谁能指出我错在哪里吗?

最佳答案

我尝试了几种方法

<androidx.core.widget.NestedScrollView
    android:id="@+id/edit_scrollView"
    app:layout_constraintTop_toBottomOf="@id/edit_point_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constrainedHeight="true"
    app:layout_constraintBottom_toBottomOf="parent">

我认为这会给你想要的设计,但说实话,我不太明白为什么它有效而 0dp 不起作用。对我来说最奇怪的是,当 View 在正常布局中膨胀时 0dp 起作用,但在 AlertDialog 中膨胀时却不起作用。

关于java - NestedScrollView 未显示在 AlertDialog 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60819528/

相关文章:

java - 给定窗框内的两个点(x0,y0 和 x1,y1) - 您将使用什么方法将一条线延伸到窗口的边缘?

java - 创建 ImageIcon 时 getClass().getResource(...) 做了什么?

android - 定期任务的 ScheduledThreadPoolExecutor(使用 Retrofit)只触发一次,永远不会再触发

IntelliJ IDEA 上的 Android Maven 项目找不到 R 类

c# - 尝试存档 Xamarin 应用程序以进行发布时找不到程序集 Mono.Android

java - 使用 GMImagePickerController 播放视频时黑屏

java - 为什么 CodePro junit 测试方法会抛出异常(在 Eclipse 中)?

java - OpenDJ LDAP 中的组和角色是一样的吗?

android - 改变TextInputLayout的 float 标签文本的位置

android - 如何在 Android 中实现谷歌地图切片缓存?