2013-07-19 41 views
3

我總是創建自定義對話框沒有所有權,使其居中(水平和垂直)使用styles.xmlrequestWindowFeature(Window.FEATURE_NO_TITLE)android:windowNoTitle但我的一些對話都沒有居中水平,例如此​​對話框:無標題的Android對話框不居中水平

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="center"  
android:padding="20dp" 
android:gravity="center" 
android:background="@drawable/dialog_bg" > 

<include 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    layout="@layout/loading_s"/> 

<TextView 
    android:id="@+id/message" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="20dp" 
    android:gravity="center_vertical" 
    android:text="@string/loading" 
    android:textColor="@color/dialog_text" 
    android:textSize="@dimen/dialog_title_text_size" /> 

</LinearLayout> 

這是如何創建對話框:

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) 
{ 
    View v = LayoutInflater.from(getActivity()).inflate(R.layout.dlg_progress, null); 
    Dialog dlg = new Dialog(getActivity(), R.style.My_Dialog_Style); //My_Dialog_Style contains android:windowNoTitle = true 
    dlg.setContentView(v); 
    dlg.setCanceledOnTouchOutside(false); 
    dlg.setCancelable(true); 
    return dlg; 
} 

這裏是h嗷嗷它出現在屏幕

enter image description here

如果我刪除android:windowNoTitle正確地將這個對話框顯示所以使用對話框沒有標題的時候只出現問題。

有誰知道爲什麼會發生這種情況,以及如何使對話框始終集中在屏幕上?

+0

如何創建對話框?這是一個'DialogFragment'嗎? – brillenheini

+0

是的,這是一個DialogFragment,請參閱更新以瞭解如何創建對話框。 – Wayne

回答

1

你試過看這個帖子嗎?

How to align custom dialog centre in android ?

android:layout_gravity="center" 

它看起來像它只是一個佈局的變化,或者嘗試使用的RelativeLayout或LinearLayout中代替的FrameLayout

+0

你可以看到我已經使用過'android:layout_gravity =「center」'。我的許多對話框使用'LinearLayout'作爲對話框容器,但它顯示正確,所以我不認爲問題是由LinearLayout造成的。 – Wayne

2

當您使用Builder,並設置有setView自定義視圖,它不應該必須刪除對話框的標題,對話框應居中。

public Dialog onCreateDialog(Bundle savedInstanceState) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    LayoutInflater inflater = getActivity().getLayoutInflater(); 
    builder.setView(inflater.inflate(R.layout.dlg_progress, null)); 
    return builder.create(); 
} 

這是非常相似,它是如何在文檔完成:Creating a Custom Layout

+0

謝謝,但我想創建一個完全自定義和複雜的對話框,所以我需要使用Dialog,而不僅僅是AlertDialog.Builder。 – Wayne

+0

@Wayne然後,您可以使用'onCreateView',就像使用普通片段一樣,而不是'onCreateDialog'。要刪除標題,請使用'setStyle(STYLE_NO_TITLE,0)'。 – brillenheini

+0

我已經嘗試過,但結果相同:( – Wayne

1

我相信你在運行的下對話框的最小寬度屬性。它可以被發現爲

<item type="dimen" name="dialog_min_width_major">65%</item> 

在Android的框架中。它取決於您正在查看的values文件夾,因此它根據密度,方向等而有所不同。

您可能可以在您的樣式中覆蓋此值。如果將它設置爲比對話框(10%)小的東西,它可能會正常工作。如果沒有,請繼續閱讀。

如果您注意到您的視圖樹面板中,它會顯示您的LinearLayout嵌套在3個FrameLayouts內。我的猜測是,最深的FrameLayout將其寬度設置爲wrap_content,所以它不會填充父級佈局,並且只與LinearLayout一樣大。不過,我不能確定,因爲尺寸在你的照片中被刪除。

爲什麼當您刪除標題時會發生變化?我不知道。你可以通過調整onMeasure中的填充/佈局參數來破解它,但似乎應該有一個更清晰的方法來做到這一點。

+0

感謝您的回答,但'android:windowMinWidthMajor'和對話框min_width仍然沒有幫助 – Wayne

+0

嗯。是不是有一個原因,你不只是使用一個像brillenheini一樣的'Builder'建議?這是我知道製作無標題對話框最簡單的方法,它正好適合我。 – Geobits

+0

因爲我想創建一個完全自定義的對話框,所以我需要使用Dialog而不是AlertDialog。上面的進度對話框只是一個例子,我有很多複雜的對話框。 – Wayne

0

仍然不知道爲什麼刪除標題讓Dialog不是水平居中,但是當我設置的LinearLayout =對話框minWidthmin_width ATTR這個問題消失。