getHeight() 获取不到布局的高度

getHeight() doesn't get the height of the layout

我要使用对话框片段。

dialogfragment中有一个名为writing_comment_item.xml的项目,我想设置height5个项目。

我用了inflate()函数,我用了getHeight()来获取布局。

但是调试的结果是,不管多高都是0

我试过 onCreate()。也尝试了 onCreateView()

结果还是一样

我该怎么办?

writing_comment_item.xml

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".fragment.WritingCommentDialogFragment">
   
    <EditText
        android:id="@+id/comment_edit"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:gravity="center_vertical"
        android:drawableLeft="@drawable/ic_bullet_point"
        android:drawablePadding="5dp"
        android:layout_marginHorizontal="10dp"
        android:background="@null"
        android:textSize="15sp"
        android:inputType="text"
        android:maxLines="1"
        android:maxLength="22"
        android:imeOptions="actionNext"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    </EditText>
</androidx.constraintlayout.widget.ConstraintLayout>

WCDialogFragment.java

public class WritingCommentDialogFragment extends DialogFragment implements CommentModel.EditInputListener {
    OnBackPressedCallback callback;

    LinearLayout commentContainer; // input layout
    private final List<Comment> comments = new ArrayList<>();
    private LayoutInflater layoutInflater;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_writing_comment_dialog, container, false);

        bindViews(view);
        addCommentItem();
        return view;
    }

    private void bindViews(View v) {
        commentContainer = v.findViewById(R.id.container);
        layoutInflater = LayoutInflater.from(getContext());
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.setCanceledOnTouchOutside(false);
        return dialog;
    }

    @Override
    public void onResume() {
        super.onResume();
        setDialogSize();
    }

    private void setDialogSize() {
        View v = LayoutInflater.from(getContext()).inflate(R.layout.writing_comment_item, null, false);
        int h = v.getHeight() * 5;

        getDialog().getWindow().setLayout(1000, h);
    }
}

已编辑

private void setDialogSize() {
        View v = LayoutInflater.from(getContext()).inflate(R.layout.writing_comment_item, null, false);
        v.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        v.getMeasuredHeight();
        int h = v.getMeasuredHeight() * 5;
        getDialog().getWindow().setLayout(1000, h);
    }

已添加图片

这里的错误是试图获取尚未添加到布局层次结构中的视图的高度。当你打电话

inflate(R.layout.writing_comment_item, null, false);

attachToRootfalse,您指定不希望将其添加到层次结构中,因此系统从不测量视图。

您可以自己调用 View.measure(int, int),然后使用 View.getMeasuredHeight()。这个问题更详细地涵盖了这一点 - Measuring a view before rendering it