【问题标题】:Android Dialog title alignmentAndroid Dialog标题对齐
【发布时间】:2018-01-13 10:03:36
【问题描述】:

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

这是风格:

<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 和其他一些项目,但没有结果。

【问题讨论】:

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


【解决方案1】:

您不能将普通警报对话框的标题居中。您将需要创建一个自定义对话框,最好使用对话框片段或对话框活动,具体取决于您需要的功能。

【讨论】:

    【解决方案2】:

    在 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

    【讨论】:

    • 你想通过 android:bottomOffset 设置什么?你是说 android:layout_marginBottom 吗?
    【解决方案3】:

    如果警报对话框在没有任何自定义布局或主题的情况下是正常的,那么您可以通过使用 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);
    

    【讨论】:

    • 我想在样式文件中做。
    【解决方案4】:
    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);
            }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-01
      • 2019-03-22
      • 2019-12-22
      • 2013-11-11
      • 2013-08-15
      • 2017-09-08
      • 1970-01-01
      相关资源
      最近更新 更多