android - 具有 ConstraintLayout 对话框宽度的 AppCompatDialogFragment

标签 android android-constraintlayout appcompatdialogfragment

我正在尝试将 AppCompatDialogFragment 与根布局作为 ConstraintLayout。

根布局的 layout_width 设置为 wrap_content。 但是当我执行代码时,即使内容发生变化,对话框也会缩小为固定宽度。

这是生成的布局代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                             xmlns:app="http://schemas.android.com/apk/res-auto"
                                             xmlns:tools="http://schemas.android.com/tools"
                                             android:layout_width="wrap_content"
                                             android:layout_height="wrap_content"
                                             android:orientation="vertical"
  >

  <TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras tempus tristique ornare. Aenean ut magna in diam aliquet accumsan. "
    android:textSize="36sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>

这是我如何膨胀布局:

View view = inflater.inflate(R.layout.weather_dialog, container, false);

Long text

Short text

我们可以看到,对于长文本,对话框会达到某种“最大宽度”(即使没有该属性)。

我怎样才能让对话框在没有这个“限制”的情况下按宽度包装其内容?

最佳答案

有一对 Window 样式属性,名为 android:windowMinWidthMajorandroid:windowMinWidthMinor

The minimum width the window is allowed to be, along the major [minor] axis of the screen

(引自documentation)

如果您通过定义一些样式为您的Dialog 使用Theme

<style name="FragmentDialogStyle" parent="Theme.AppCompat.Dialog">
    <item name="windowMinWidthMajor">@dimen/dialog_fragment_width</item>
    <item name="windowMinWidthMinor">@dimen/dialog_fragment_width</item>
    <item name="android:windowMinWidthMajor">@dimen/dialog_fragment_width</item>
    <item name="android:windowMinWidthMinor">@dimen/dialog_fragment_width</item>
</style>

和设置

<item name="android:dialogTheme">@style/FragmentDialogStyle</item>
<item name="dialogTheme">@style/FragmentDialogStyle</item>

在应用程序 Theme 中,Dialog 宽度将恰好是 dialog_fragment_width 如果您还使用

android:layout_width="0dp"

使用您的 TextView

而如果你继续使用

android:layout_width="wrap_content"

您观察到的“最大宽度”将限制您的Dialog 宽度。

顺便说一句,如果您为 TextViewConstraintLayout 的背景着色,您将仅在使用 0dp 宽度时观察到value 所有边距都是正确的。

看来您最好的选择是为 TextView 设置 android:layout_width="0dp" 并为 dialog_fragment_width 提供不同的值>,取决于当前屏幕(对于 Nougat+:应用程序窗口)宽度。为此,您需要不同的目录,例如 res/values-w600dp

请注意:我使用模拟器测试了运行 Marshmallow 的平板电脑和运行 Lollipop 的手机。虽然它有效,但如果我也能解释原因,我会更高兴。所以我认为这更像是答案的一半,但我想分享我的观察。

关于android - 具有 ConstraintLayout 对话框宽度的 AppCompatDialogFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40151278/

相关文章:

android - 如何在 ConstraintLayout 中为 subview 赋予重力

android - appcompat-v7 :23. 1.0 的 AppCompatDialogFragment 问题

android - AppCompatDialogFragment 更改背景颜色

java - 更改整个应用程序的背景

java - 以编程方式添加锁屏通知Android的权限

android - 约束布局中 0dp 的对话框布局高度不起作用

android - 在 ConstraintLayout 中以编程方式添加 View

java.util.zip.ZipException : duplicate entry :how to overcome

android - 带有移动 float 操作按钮的 Bottom Sheet

android - DialogFragment 的生命周期是什么