【问题标题】:ScrollView containing a TextView disappears in ConstraintLayout when height is set to 0dp当高度设置为 0dp 时,包含 TextView 的 ScrollView 在 ConstraintLayout 中消失
【发布时间】:2022-09-26 09:02:11
【问题描述】:

我正在尝试使用 ConstraintLayout 创建一个自定义 alertDialog,其中包含标题、带有 textview 的滚动视图和底部的按钮。

我希望滚动视图动态增长/缩小并填充标题和按钮之间的可用空间,这在预览中有效,但是在运行实际应用程序时,滚动视图和文本视图似乎实际上缩小到 0dp 并消失(据我所知它,带有 fillViewport 的 0dp 应该根据约束增长以适应可用空间。

Preview

Actual app

布局:

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"
    android:layout_width=\"match_parent\"
    android:layout_height=\"match_parent\"
    xmlns:tools=\"http://schemas.android.com/tools\"
    xmlns:app=\"http://schemas.android.com/apk/res-auto\"
    android:background=\"@color/white\">
    <TextView
        android:id=\"@+id/tvTitle\"
        android:layout_width=\"0dp\"
        android:layout_height=\"wrap_content\"
        android:text=\"A title\"
        android:textSize=\"20dp\"
        android:gravity=\"start\"
        android:textColor=\"#000000\"
        app:layout_constraintStart_toStartOf=\"parent\"
        app:layout_constraintEnd_toEndOf=\"parent\"
        app:layout_constraintTop_toTopOf=\"parent\"
        />
    <ScrollView
        android:id=\"@+id/svBody\"
        android:layout_width=\"0dp\"
        android:layout_height=\"0dp\"
        android:fillViewport=\"true\"
        app:layout_constraintTop_toBottomOf=\"@id/tvTitle\"
        app:layout_constraintBottom_toTopOf=\"@id/btnClose\"
        app:layout_constraintLeft_toLeftOf=\"parent\"
        app:layout_constraintRight_toRightOf=\"parent\"
        >
        <TextView
            android:id=\"@+id/tvBody\"
            android:layout_width=\"match_parent\"
            android:layout_height=\"wrap_content\"
            android:text=\"AA text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long. text that is really long.\"
            android:textSize=\"16dp\"
            android:textColor=\"#000000\"
            />
    </ScrollView>
    <Button
        android:id=\"@+id/btnClose\"
        android:layout_width=\"100dp\"
        android:layout_height=\"40dp\"
        android:text=\"close\"
        app:layout_constraintStart_toStartOf=\"parent\"
        app:layout_constraintEnd_toEndOf=\"parent\"
        app:layout_constraintBottom_toBottomOf=\"parent\"
        android:layout_margin=\"5dp\"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

这可能是由于与稍后负责呈现此自定义警报对话框的视图的约束发生冲突引起的吗?还是我错过了其他东西?

我当然可以通过在 scrollView 上设置固定大小来解决它,但是在更大的屏幕上它看起来很小,所以我希望有一个动态的解决方案。

    标签: android textview scrollview android-constraintlayout


    【解决方案1】:

    所以我修复了它,如果你将值设为 match_parent,而不是 0dp,它会起作用:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:background="@color/white">
        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="A title"
            android:textSize="20dp"
            android:gravity="start"
            android:textColor="#000000"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            />
        <ScrollView
            android:id="@+id/svBody"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            app:layout_constraintTop_toBottomOf="@id/tvTitle"
            app:layout_constraintBottom_toTopOf="@id/btnClose"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            >
            <TextView
                android:id="@+id/tvBody"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="AA text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long. text that is really long."
                android:textSize="16dp"
                android:textColor="#000000"
                />
        </ScrollView>
        <Button
            android:id="@+id/btnClose"
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:text="close"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_margin="5dp"
            />
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    其他选项,使用 PopupWindow 而不是 AlertDialog (您可以像以前一样正常插入的弹出视图):

        int width = LinearLayout.LayoutParams.MATCH_PARENT;
        int height = LinearLayout.LayoutParams.MATCH_PARENT;
        boolean focusable = true; // lets taps outside the popup also dismiss it
        final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
            
        // show the popup window
        // which view you pass in doesn't matter, it is only used for the window tolken
    
    popupWindow.showAtLocation(getWindow().getDecorView().findViewById(android.R.id.content), Gravity.CENTER, 0, 0);
            
        // dismiss the popup window when touched
        popupView.setOnTouchListener(new View.OnTouchListener() {
             @Override
             public boolean onTouch(View v, MotionEvent event) {
                  popupWindow.dismiss();
                  return true;
             }
        });
    

    【讨论】:

    • 感谢您的回复!不幸的是,匹配父匹配父也不起作用,因为如果文本大于全屏,这会使文本出现在标题和按钮的后面。我将查看弹出窗口,看看是否有效!
    • PopupWindow 确实有效,谢谢!虽然它出现在之前的弹出窗口后面,但这是由于我的实现。对我来说,我认为它也应该与 alertDialog 一起使用,但在这种情况下似乎存在某种错误。非常感谢您的帮助!
    【解决方案2】:

    可能您的对话框没有父级。因此 ConstraintLayout 的 match_parent 不起作用。所以对话框仅限于设置了 wrap_content 的元素。如果您在具有活动的普通片段中显示整个内容,则它可以工作。 因此,我会尝试使用 DialogFragment。在那里它应该可以正常工作。此外,它看起来更漂亮。

    相关文档: https://developer.android.com/guide/fragments/dialogs

    【讨论】:

    • 感谢你的回复!我认为它应该有一个父级,它是在一个本身具有约束布局的活动中创建和显示的。像这样: View popup = inflater.inflate(R.layout.my_custom_layout, null); final AlertDialog alertDialog = new AlertDialog.Builder(this).create(); alertDialog.setCancelable(false); alertDialog.setView(popup); alertDialog.show();
    • 似乎约束布局中存在错误或布局高度无法应用于滚动视图中的约束布局,但您可以在约束布局中使用最小高度属性。尝试设置最小高度: android:minHeight="500dp" 可能还有布局高度: android:layout_height="500dp"
    • 更好的是在滚动视图之外放置一个 ContraintLayout。看这里:stackoverflow.com/a/44843684/13359215
    • 嗯很有趣。问题是我不想为高度设置一个固定的 dp 值,我希望它适用于小型 800x480 设备以及较新的高 DPI 设备。但我会尝试添加一个内部约束布局,就像你链接的线程一样,谢谢!我会报告情况如何。
    • 不幸的是,同样的问题。我也在我的线程中尝试了另一个答案,使用可滚动的文本视图而不是滚动视图,但即便如此它仍然会消失。
    【解决方案3】:

    我认为这应该可以解决您的问题。

    https://stackoverflow.com/a/3256305/13303290

    【讨论】:

    • 不幸的是同样的问题。删除了滚动视图并将其添加到文本视图中,但它仍然没有出现。
    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-25
    相关资源
    最近更新 更多