android-studio - Android Studio:无法将 DialogFragment 的膨胀布局获取到 match_parent
问题描述
我有一个片段,上面显示了一个 [弹出] DialogFragment。问题是我无法获得我正在膨胀以填充 DialogFragment 的 RelativeLayout。实际上,它总是水平填充;从不垂直。
[popup] DialogFragment (RelativeLayout) 具有layout_width
并layout_height
设置为"match_parent"
.
以下屏幕截图显示了单独的示例。在每种情况下,白色矩形都是(大小正确的)DialogFragment。膨胀的 XML 是红色和黄色部分(带有 TextView)。这个红色和黄色来自背景可绘制对象。
请注意,膨胀的 RelativeLayout总是占据整个宽度,而不是整个高度。请注意,我移动了嵌入的 TextView(对于最后两个屏幕截图)来说明两点:
(1) 这个 TextView 的位置决定了膨胀布局的高度。
(2) 它的位置对宽度没有影响。
另请注意:
TextView 的
layout_alignParentStart
和layout_alignParentTop
都是true
如果我完全删除 TextView,膨胀的布局“消失”(大概是因为我现在的“高度”为零)。
屏幕截图 #2 和 #5(第二列)是相同的 - 除了 TextView 的位置。
#3 和#6(第三列)也是如此。
(TextView 在屏幕截图 #6 中的红色区域大部分是垂直的。在这里很难看到,但这就是它所在的位置 - 黄色的右侧,其底部与黄色区域的底部对齐。)
我错过了什么/做错了什么?
代码块:
ZzzDialog.kt([弹出] DialogFragment 的来源):
package com.zazzem.two.dialogs
import android.app.Dialog
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
class ZzzDialog : DialogFragment() {
private lateinit var popup: View
private var popupLft: Int = 0
private var popupTop: Int = 0
private var popupWid: Int = 0
private var popupHgt: Int = 0
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val inflater = LayoutInflater.from(context)
popup = inflater.inflate(com.zazzem.two.R.layout.dialog_zzz, null)
return activity!!.let {
val builder = AlertDialog.Builder(it)
builder.setView(popup)
builder.create()
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val win = dialog!!.window!!
win.setGravity(Gravity.TOP or Gravity.LEFT) //top left first, so x & y below will be relative to there
return super.onCreateView(inflater, container, savedInstanceState)
}
override fun onResume() {
super.onResume()
val win = dialog!!.window ?: return
val params = win.attributes
params.x = popupLft
params.y = popupTop
params.width = popupWid
params.height = popupHgt
win.attributes = params
}
fun setPosAndSize(lft: Int, top: Int, wid: Int, hgt: Int) {
popupLft = lft
popupTop = top
popupWid = wid
popupHgt = hgt
}
}
[TEMP] ClickListener(调用“setup”并显示 [popup] 的地方):
btnDevTemp.setOnClickListener {
fun doOne(l: Int, t: Int, w: Int, h: Int) {
dialog_specs.text = "Lft:$l Top:$t Wid:$w Hgt:$h"
val dlg = ZzzDialog()
dlg.setPosAndSize(l, t, w, h)
dlg.show(activity!!.supportFragmentManager, "ZzzDialog")
}
doOne(100,200,800,800) //pic #1
//doOne(10,10,1200,1900) //pic #2 and #5
//doOne(500,500,800,1200) //pic #3 and #6
//doOne(300,300,450, 900) //pic #4
}
[弹出] DialogFragment 的 XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bkgr_zzz"
android:orientation="horizontal"
>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="151dp"
android:layout_marginTop="112dp"
android:backgroundTint="@color/Pink"
android:text="TextView"
/>
</RelativeLayout>
以上的XMLbackground
:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<!-- yellow -->
<solid android:color="#FFFF00" />
<padding
android:bottom="20dp"
android:left="20dp"
android:right="20dp"
android:top="20dp"
/>
<!-- red -->
<stroke
android:width="25dp"
android:color="#FF0000"
/>
</shape>
解决方案
尝试这个。
像这样以编程方式获取对话框片段的高度:
int height = getDialog().getWindow().getDecorView().getHeight();
然后,获取该父 RelativeLayout 的引用并以编程方式设置它的高度。
relativeLayout.getLayoutParams().height = height;
推荐阅读
- c++ - Ifstream 在 OSX 上看不到新内容
- javascript - 如何确定模块需要导入或需要的内容?
- mysql - 如何在 MySQL 中使用条件加入
- java - 创建包含用户特定数据的 WebSocket 会话的最安全方法
- jpa - 使用入口集的值无效
- python - Pandas 如何确定某事物是否为“标量”?
- atlassian-sourcetree - 数据库中是否需要 .git 文件夹?
- d3.js - 3 级嵌套数据的 D3v4 更新模式遇到问题
- python - 为给定的值范围生成给定大小的所有可能的元组组合
- vue.js - 量角器 - 使用 by.id 时“为定位器找到多个元素”