0

如何居中垂直「登錄」標題?Android對話框標題對齊

這是風格:

<style name="ABGAlertDialog" parent="@android:style/Theme.DeviceDefault.Dialog"> 
    <item name="android:textColor">@android:color/black</item> 
    <item name="android:textColorHint">@android:color/darker_gray</item> 
    <item name="android:background">@color/corporativo_red</item> 
    <item name="android:windowTitleStyle">@style/MyAlertDialogTitle</item> 
</style> 

<style name="MyAlertDialogTitle"> 
    <item name="android:background">@color/corporativo_red</item> 
    <item name="android:colorBackground">@color/corporativo_red</item> 
    <item name="android:textColor">@android:color/white</item> 
    <item name="android:textStyle">bold</item> 
    <item name="android:maxLines">1</item> 
    <item name="android:textSize">20sp</item> 
    <item name="android:bottomOffset">5sp</item> 
</style> 

我試過gravitytextAlignment和其他一些項目沒有結果。

+0

你在哪裏設置'MyAlertDialogTitle'風格? –

+0

嘗試更改底部偏移量至50sp – Mushroomzier

+0

@user如果您想要使用自定義佈局,請發佈您的XML文件.. – user1140237

回答

1

您無法居中正常警報對話框的標題。您將需要創建一個自定義對話框,最好根據您需要的功能使用對話框片段或對話框活動。

0

設置在Android的文本樣式:textAppearance

<style name="MyAlertDialogTitle"> 
    <item name="android:background">@color/corporativo_red</item> 
    <item name="android:colorBackground">@color/corporativo_red</item> 
    <item name="android:bottomOffset">5sp</item> 

    <item name="android:textAppearance">@style/YourTextStyle</item> 
</style> 

Change both text and background color in title bar (Android)?
Textview: using the dialog title style: @android:style/TextAppearance.DialogWindowTitle

+0

android:bottomOffset沒有效果。 – user2655890

+0

你想通過android設置:bottomOffset?你的意思是android:layout_marginBottom? –

0

如果警報對話框是正常的,沒有任何自定義佈局或主題的比你CA通過傳遞ID與獲取對象android命名空間

AlertDialog.Builder alertDialogBuild = new AlertDialog.Builder(getActivity()); 
       alertDialogBuild.setMessage("message text align center"); 
       alertDialogBuild.setTitle("Title text Align Center"); 
       alertDialogBuild.setPositiveButton("Okay", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 

        } 
       }); 
       AlertDialog dialog = alertDialogBuild.show(); 
       TextView title = (TextView) dialog.findViewById(getActivity().getResources().getIdentifier("alertTitle", "id", "android")); 
       title.setGravity(Gravity.CENTER); 


       TextView message = (TextView) dialog.findViewById(getActivity().getResources().getIdentifier("message", "id", "android")); 
       message.setGravity(Gravity.CENTER); 
+0

我想在樣式文件中做到這一點。 – user2655890

+0

你可以發表你的代碼來顯示對話框佈局嗎? – user1140237

0
static void centerTitleHorizontally(final TextView title) 
{ 
    ViewTreeObserver vto = title.getViewTreeObserver(); 
    vto.addOnGlobalLayoutListener (new ViewTreeObserver.OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 

      try { 
       title.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
      } catch (NoSuchMethodError x) { 
       title.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
      } 

      Rect bounds = new Rect(); 
      title.getPaint().getTextBounds(title.getText().toString(), 0, title.getText().length(), bounds); 
      title.setPadding((title.getWidth() - bounds.width())/2, 0, 0, 0); 
     } 
    }); 
}