Android 为什么我的对话片段显示得这么薄?
我有一个简单的Android 为什么我的对话片段显示得这么薄?,android,android-layout,android-dialogfragment,Android,Android Layout,Android Dialogfragment,我有一个简单的对话框片段,其中包含3个编辑文本s和一个按钮。我已经设置了其主布局的布局宽度350dp,但当我运行项目时,它将显示得如此纤细 这是我的DialogFragment XML: <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.androi
对话框片段
,其中包含3个编辑文本
s和一个按钮
。我已经设置了其主布局的布局宽度350dp
,但当我运行项目时,它将显示得如此纤细
这是我的DialogFragment XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:animateLayoutChanges="true"
android:background="#AFE6FF"
android:orientation="vertical"
android:padding="24dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.constraint.ConstraintLayout
android:id="@+id/layout_mobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="@+id/et_mobile"
android:layout_width="match_parent"
android:layout_height="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:id="@+id/layout_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="@+id/et_title"
android:layout_width="match_parent"
android:layout_height="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:id="@+id/layout_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/layout_title">
<EditText
android:id="@+id/et_body"
android:layout_width="match_parent"
android:layout_height="150dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
<Button
android:id="@+id/btn_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/layout_body" />
</LinearLayout>
</ScrollView>
我需要它显示得更宽。您需要更改对话框的
布局参数,如下面的代码所示。将其放入onResume()
:
我认为默认对话框的LayoutParams(wrap\u content
,wrap\u content
)覆盖了XML中的定义
最好这样使用,不要使用固定的大小值(350dp
)来保持版面对所有屏幕大小的响应。您希望覆盖onCreateDialog
并在其中添加自定义版面。如下图所示。我使用ViewBinding创建视图
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return AlertDialog.Builder(requireContext())
.setView(binding.root)
.setCancelable(false)
.create().apply {
setCanceledOnTouchOutside(false)
}
}
如何尝试更改设置为滚动视图的宽度?说到这个安卓:layout_width=“350dp”,试着把它改成一个更大的数字,直到我知道它是否改变了什么。请张贴“调用”活动的代码dialogue@Dor它不会改变结果。我将您的代码放在onResume()
中,它工作了,但现在它像一个活动一样全屏显示,因为它的宽度和高度设置为匹配\u PARENT
。我可以为LinearLayout
设置固定宽度,并设置lp.width
和lp.height
来包装内容,但我想知道是否有更好的标准方法?也许您可以这样做-lp.height=(int)(WindowManager.LayoutParams.MATCH\u PARENT*0.5)代码>只需更改0.5以更好地控制您的参数您知道。。。我很好奇为什么会这样?这不是我在这个项目中创建的第一个DialogFragment
!!!我真的没有一个答案适合所有的情况,有时当使用对话框时,所有的都工作了,而另一次我遇到了与您相同的问题,我需要使用布局婴儿车,正如我在回答中提到的那样来解决它。哦,我错了!我曾经把getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_父项,ViewGroup.LayoutParams.MATCH_父项)放在代码>也在我的其他片段对话框中!
Window window = yourDialog.getWindow();
if (window != null) {
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setAttributes(lp);
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return AlertDialog.Builder(requireContext())
.setView(binding.root)
.setCancelable(false)
.create().apply {
setCanceledOnTouchOutside(false)
}
}