【问题标题】:getHeight() doesn't get the height of the layoutgetHeight() 没有得到布局的高度
【发布时间】:2021-03-08 18:46:39
【问题描述】:

我将使用对话框片段。

dialogfragment中有一个叫writing_comment_item.xml的项目,我想设置5项目中的height

我使用了 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);
    }

添加图片

【问题讨论】:

  • 这是否回答了您的问题:stackoverflow.com/a/3594216/4303296 ?
  • 这能回答你的问题吗? View's getWidth() and getHeight() returns 0
  • @Beko 我也从链接中看到了答案。但是我不能只得到一个项目的高度......我已经添加了代码
  • 我以前也遇到过这个问题。不幸的是,我认为没有什么比上面链接中已经讲述的要多说的了。我认为您必须通读这些答案和 cmets,并尝试找出适合您的答案。祝你好运。
  • 查看更新后的代码,您似乎在夸大布局但没有使用它?但是想利用它的高度?

标签: android android-view appcompatdialogfragment


【解决方案1】:

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

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

如果attachToRootfalse,则表示您不希望将其添加到层次结构中,因此系统永远不会测量视图。

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

【讨论】:

  • 哇,谢谢,几乎解决了。几乎意味着因为有一点问题。我想将项目的高度设置为 5,所以我将使用 getMeasuredHeight() 获得的值乘以 5。但是,结果并不是全部 5 项,而是有点截断。换句话说,对话框的大小略小。你知道为什么吗?我编辑了代码
  • 您的代码实际上似乎没有使用测量的高度?
  • 准确来说似乎是在使用高度。不过,也不是五人的高度。该项目有点切断。我发图片
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-10
  • 1970-01-01
  • 1970-01-01
  • 2020-12-06
  • 2012-03-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多