2017-08-05 61 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