Android对话框标题对齐方式

时间:2015-12-18 10:43:21

标签: android android-layout android-alertdialog android-styles

如何将“登录”标题垂直居中?

这是风格:

<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以及其他一些没有结果的项目。

4 个答案:

答案 0 :(得分:1)

您无法居置正常的提醒对话框标题。您需要创建一个自定义对话框,最好使用对话框片段或对话框活动,具体取决于您需要的功能。

答案 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

答案 2 :(得分:0)

如果警报对话框是正常的,没有任何自定义布局或主题,则通过使用android命名空间传递id来获取对象

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);

答案 3 :(得分: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);
        }
    });
}