【问题标题】:Improper text appearance in Custom AlertDialog自定义警报对话框中的文本外观不正确
【发布时间】:2018-12-12 14:28:06
【问题描述】:

在我的代码中,我通过扩展布局并将其设置为对话框的视图来呈现自定义警报对话框。 我遇到的问题是文本在 Android Studio 的设计工具中看起来不错,但在运行时,对话框变小并且文本占用更多行。 我尝试了不同的方法来修复它,但没有获得想要的结果。

对于布局,我使用的是约束布局,文字是"wrap content"

这是我的代码:

    //inflate alert layout
    LayoutInflater inflater = LayoutInflater.from(this);
    final View view = inflater.inflate(R.layout.connectivity_issue_counter, null);
    //set builder
    AlertDialog.Builder dialog = new AlertDialog.Builder(this);
    dialog.setView(view);
    dialog.setCancelable(false);

    final AlertDialog alert = dialog.create();
    //define dialog buttons and counter......//
    alert.show();

XML:

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/alertBackground">

    <TextView
        android:id="@+id/connectivity_issue_title"
        style="@style/customAlertTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="50dp"
        android:text="@string/connectivity_issue_title"
        app:layout_constraintEnd_toStartOf="@+id/connectivity_issue_wait"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/connectivity_issue_wait"
        style="@style/customAlertTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:text="@string/connectivity_issue_wait"
        app:layout_constraintEnd_toStartOf="@+id/connectivity_issue_counter_text"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/connectivity_issue_counter_text"
        style="@style/customAlertTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/connectivity_issue_main_text"
        style="@style/customAlertMainText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:text="@string/connectivity_issue_main_text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/connectivity_issue_title" />

    <Button
        android:id="@+id/connectivity_isuue_button"
        style="@style/customAlertButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:text="@string/stop"
        app:layout_constraintEnd_toStartOf="@+id/guideline21"
        app:layout_constraintStart_toStartOf="@+id/guideline22"
        app:layout_constraintTop_toBottomOf="@+id/connectivity_issue_main_text" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline20"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.82" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline21"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.75" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline22"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.25" />

</android.support.constraint.ConstraintLayout>

*另一件有趣的事 - 在不同的平板电脑上(都是同一型号),对话框看起来越来越宽。

我将不胜感激。

【问题讨论】:

  • 能否请您也提供一下您的布局代码?
  • 已在帖子中更新
  • 好的,谢谢 Itay。

标签: android dialog android-alertdialog android-constraintlayout layout-inflater


【解决方案1】:

正如 Davidaz 所建议的,我已将文本大小从 sp 更改为 dp,这有助于解决问题。

【讨论】:

    【解决方案2】:

    ¿也许那部手机上的文字更大?我看不到customAlertTitle 样式,但我想文本在“SP”上,这些是“DP”,但取决于文本大小(如果手机设置为在其设置中以大字体显示文本选项卡,它会增加它的大小)。

    也许解决方案是将尺寸设置为“DP”,但您不会让人们更改它,因此会产生反效果。

    我想到的另一个想法是,您在预览中预览的屏幕尺寸比您手机的屏幕大。

    我要尝试的最后一件事是在创建对话框后手动更改“android.R.id.message”TextView 的大小。一旦你显示它,你就可以通过调用它的 id 来访问消息的文本视图,

    TextView messageTv = (TextView) alert.findViewById(android.R.id.message);
    messageTv.setTextSize(X);
    

    编辑:不要介意最后一个想法,因为您正在膨胀自己的布局,也许您可​​以尝试访问您的 id 并以相同的方式更改它,但使用R.id.connectivity_issue_wait

    【讨论】:

    • 谢谢大卫!文本确实在 SP 中,我将尝试将其更改为 DP。预览与平板电脑大小相同。如果 DP 的东西不起作用,我会尝试这种方法。
    • 没问题,很高兴它帮助了你!但是请记住,这不是一个好的做法,即使您将其设置为 DP,也有一些具有自定义配置的手机可以更改屏幕上每个细节的大小。有时最好不要试图做到像素完美,而是设计一个在每台设备上都可以很好看的布局!
    猜你喜欢
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    相关资源
    最近更新 更多