【问题标题】:Custom dialog doesn't appear to have its set width自定义对话框似乎没有设置的宽度
【发布时间】:2018-01-13 12:18:16
【问题描述】:

我有一个简单的对话框,它只显示两个选项(“编辑”和“删除”)。我在 .xml 文件中设置了它的布局,在属性中指定了给定的宽度。但是,在应用程序中,它看起来与设置的“wrap_content”一样窄。它的宽度足以包裹文本。我该如何克服这个问题?可能我错过了一些非常简单的东西。这是xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:orientation="vertical"   >
<TextView
  android:id="@+id/editOption"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginBottom="16dp"
  android:layout_marginLeft="6dp"
  android:layout_marginRight="6dp"
  android:layout_marginTop="16dp"
  android:text="@string/edit"
  android:textAlignment="center"
  android:textColor="@color/colorPrimaryText"
  android:textSize="@dimen/text_size" />

  <TextView
  android:id="@+id/deleteOption"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginBottom="20dp"
  android:layout_marginLeft="6dp"
  android:layout_marginRight="6dp"
  android:layout_marginTop="20dp"
  android:text="@string/delete"
  android:textAlignment="center"
  android:textColor="@color/colorPrimaryText"
  android:textSize="@dimen/text_size" />
  </LinearLayout>

提前致谢

【问题讨论】:

  • 它的宽度刚好可以换行。我该如何克服这个问题?你想让文本垂直或水平对齐..?
  • 垂直,我的意思是,一个在另一个之下。

标签: android xml dialog width customdialog


【解决方案1】:

尝试将布局宽度更改为最小宽度:

android:layout_width="350dp" <- instead
android:minWidth="350dp" <- insert

【讨论】:

  • 好的,minWidth属性解决了问题,谢谢!无论如何,为什么这么有必要?如果我指定的布局宽度与“wrap_content”不同,为什么它只包装内容?我想了解这个逻辑。
  • 这取决于 Android 版本、设备、主题。 Android 由于要支持的不同发行版的数量而受到阻碍。有些东西在 Pixel 上有效,但在三星、华为或 LG 上无效。对于您的示例,它无需我在 OnePlus 3T 上进行更改即可工作。
  • 感谢您的解释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-11
  • 1970-01-01
  • 2011-11-06
  • 1970-01-01
  • 2013-05-16
相关资源
最近更新 更多