Android 警报对话框 - 调整文本大小? (改变方向)

Android Alert Dialog - Resize text ? (Change Orientation)

我正在学习 Android 并且我正在努力避免在我的应用程序中出现这种行为。

这是在对话框中使用 setMessage 的结果。

这是在对话框中使用 setTittle 的结果。

有没有办法避免当我将方向更改为水平时文本或单选按钮被剪切?

我在这个警告对话框中使用自定义布局 (LinearLayout) 来显示单选按钮。

我还使用 onCreateDialog 来创建警报对话框。

@Override
protected Dialog onCreateDialog(int id) {

    Dialog createdDialog;
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    toDisplayInDialog = getLayoutInflater().inflate(R.layout.light_radiogroup, null);
    builder.setTitle("Choose Startup Color:")
           .setPositiveButton("Set Color",
           new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // Do things on Click
               }
           })
           .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   dialog.cancel();
               }
            })
           .setView(toDisplayInDialog);
    createdDialog = builder.create();
    return createdDialog;
}

基本问题——您可能已经猜到了——是在横向模式下有太多控件无法容纳在屏幕上。您需要添加一个 ScrollView 以便用户可以滚动 UI。

<ScrollView 
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:text="Green"
        android:checked="true" />

   ... etc.

</ScrollView>

更新

有多种方法可以将小部件布局为两列。不幸的是,没有标准的布局可以让控件流向多列。但是,下面是显示五个单选按钮的固定方式。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Green" />
        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Black" />
        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Yellow" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Red" />
        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="White" />

    </LinearLayout>

</LinearLayout>

下面是预览:

注意:您可能不想在纵向显示时使用此布局,只希望在横向显示时使用。您可以通过添加可以添加此布局的 "layout-land" 资源目录来同时拥有两者,并在正常的 "layout" 资源目录中使用常规布局。这样系统会根据设备是纵向还是横向选择相应的布局资源。

这是两倍的工作量,但您的 UI 看起来会更好。

由于屏幕高度对于对话框来说太小,对话框需要降低其高度以适合其边距和自身填充。由于元素不再正常适合,它也需要降低它们的高度。 但是在 TextView 上,这不包括缩放,那又怎样你得到的是标题的裁剪。

这是 Android front-end 开发中的一个完全常见的问题 - 您使自己成为一个完美的设计,但在某些 sizes/densities.

上面临技术问题

你可以尝试这些(我建议你在必要时应用所有 3 个点):

  1. RadioButton 圆在缩小时裁剪。但它包含一些 built-in 填充。您可以尝试更改每个 RadioButton 的高度。您只需要确保其结果与所有密度和尺寸的屏幕兼容。

或者,您可以应用以下 3 个步骤:

1) 将每个 RadioButton、RadioGroup 和标题的 layout_height 设置为 "match_parent"

2) 将每个 RadioButton、RadioGroup 和标题的 layout_weight 设置为 "1"。单选按钮将在容器内平均分布,并且容器和标题将具有相同的高度。

3) 根据需要增加标题的layout_weight

上述方法将减小标题大小并增加 RadioGroup 大小,同时在内部均匀分布 RadioButton。

  1. 根据 RadioGroup 上方没有发生裁剪来判断,这可能意味着标题上有 padding_bottom。您应该 remove/decrease padding_bottom 从对话框标题。如果你需要在垂直模式下填充,你可以创建 2 个不同的布局,如 this answer, or take care of that programmatically through onConfigurationChanged like described here(在水平模式下删除 padding_bottom,并在垂直模式下恢复它)。

  2. 当您使用自己的对话框布局时,在我看来您的标题 layout_height 可能设置为 "match_parent""fill_parent" .您可以尝试将其替换为 "wrap_content",尽管它会产生不同的不良影响。