【发布时间】: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