2017-08-05 101 views
0

我有一个简单的对话框,它只显示两个选项(“编辑”和“删除”)。我在.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> 

感谢

+0

这只是足够宽,以换行。我怎么能解决这个问题?你希望文字垂直或水平对齐..? –

+0

垂直,我的意思是,一个在另一个之下。 – paola91

回答

0

试图改变布局宽度为最小宽度:

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

好的,属性'minWidth'解决了这个问题,谢谢! 无论如何,为什么它是如此的必要?如果我将布局宽度与“wrap_content”不同,它为什么只包装内容?我想理解这个逻辑。 – paola91

+1

这取决于Android版本,设备,主题。 由于需要支持不同的发行版本,因此Android会受到阻碍。 Pixel可以使用某些功能,但在三星,华为或LG无法使用。当涉及到您的示例时,它在OnePlus 3T上无需更改即可使用。 – Maciej

+0

谢谢你的解释。 – paola91