簡體   English   中英

自定義警報對話框未在 Android 上垂直居中

[英]Custom Alert dialog not centered vertically on Android

我制作了一個自定義警報對話框,在我的游戲結束時顯示,以便玩家可以輸入其名稱進行保存。 問題是當我在對話框上調用show()時出現但它不是垂直居中的! 它比它應該的要低一點,無論我在 xml 中設置什么屬性或在使用setGravity()

我認為這與這里提到的問題相同,但沒有人給出正確的答案。

謝謝你的幫助。

有關更多詳細信息,這是我的代碼:

    AlertDialog.Builder builder;

    LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.newrecord,(ViewGroup)findViewById(R.layout.shoot));

    builder = new AlertDialog.Builder(this);
    builder.setView(layout);

    newRecDialog = builder.create();

這是newrecord.xml的 XML 布局的第一個元素的代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
 android:padding="10dp"
android:baselineAligned="true">

這是輸出屏幕截圖: 替代文字
(來源: free.fr

hides the title panel and the dialog is centered on the screen.如果您實現自己的對話框,則隱藏標題面板,並且對話框位於屏幕 也許它也適用於 AlertDialog。

此處描述該錯誤。 即使在既沒有標題也沒有圖標的情況下,AlertDialog 也會為標題/圖標面板保留空間。

我認為修復方法很簡單:它應該將頂部面板布局設置為 GONE,就像在沒有按鈕的情況下對按鈕面板所做的那樣。 在完成之前,唯一的解決方法是實現您自己的 Dialog 子類。

為此,您可以使用 Activity 而不是自定義警報。 您必須在android清單文件中將活動主題設置為對話框:

android:theme="@android:style/Theme.Dialog"

您可以根據需要調整活動 xml 布局。

android:theme="@android:style/Theme.Dialog"

嘗試:

newRecDialog.getWindow().getAttributes().gravity = Gravity.CENTER;

將 AlertDialog 上的屬性設置為android:gravity="center"或以編程方式設置為setGravity(Gravity.CENTER) 此重力僅適用於您的布局,而不適用於您的手機顯示。 如果您使用自定義標題,它看起來不像垂直居中。

您需要禁用窗口標題。 首先為您的對話框創建自定義樣式(確保使用適合您需要的父主題):

<style name="CustomDialog" parent="@android:style/Theme.Dialog">
   <item name="android:windowNoTitle">true</item>
</style>

然后將您的樣式應用於對話框構建器:

AlertDialog.Builder builder;

LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.newrecord,(ViewGroup)findViewById(R.layout.shoot));

builder = new AlertDialog.Builder( new ContextThemeWrapper(this, R.style.CustomDialog));
builder.setView(layout);

newRecDialog = builder.create();

不是真正的答案,但我遇到了類似的問題。 看起來它是居中的,但它假設 AlerterDialog 有一個標題集。 就我而言,我只是設置了一個標題。

如果你正在處理任何高度和寬度屬性,你必須確保不要改變高度,因為它會改變位置,這里是一個示例。

     myProgressDialog.show();           
            float widthPecent = 0.60f;
            //order matters
            DisplayMetrics displayMetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
            int displayWidth = displayMetrics.widthPixels;

        //dont do any adjustments to the height. **************************  <<<<<<<<<<  

            WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
    
            layoutParams.copyFrom(myProgressDialog.getWindow().getAttributes());
            int dialogWindowWidth = (int) (displayWidth * widthPecent);
            layoutParams.width = dialogWindowWidth;

 //dont do any changes to the height. **************************  <<<<<<<<<<  

            myProgressDialog.getWindow().setAttributes(layoutParams);

layoutParams.height = dialogWindowHeight; //注釋或刪除此行。

你可以嘗試使用AlertDialog.Builder.setCustomTitle(View); 而不是setView 我們使用它是因為警報對話框看起來比帶有空標題的對話框好一點。

您應該在 xml 中使用以下行,首先從您的 xml 中刪除該填充行,然后

android:layout_gravity="center"

在此之后,您的對話框將出現在中心,如果您使用的是左邊的邊距等,那么請刪除它,如果您正在使用它

android:layout_width="fill_parent"

比改變它

android:layout_width="wrap_content"

之后,您的對話框將出現在中心。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM