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

时间:2015-01-03 12:56:59

标签: android android-alertdialog screen-orientation

我正在学习Android,并且我试图在我的应用上避免这种行为。

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

rotate

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

rotate2

当我将方向改为水平时,有没有办法避免文本或radioButtons被切断?

我使用自定义布局(LinearLayout)和此警报对话框来显示radioButtons。

我还使用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;
}

2 个答案:

答案 0 :(得分:2)

基本问题 - 正如您所猜测的那样 - 在横向拍摄时屏幕上有太多控件无法容纳。您需要添加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>

以下是预览:

Preview

注意:在纵向显示时,您可能不希望使用此布局,仅在横向显示。您可以通过添加&#34; layout-land&#34;资源目录,您可以在其中添加此布局,并在常规&#34;布局&#34;中使用常规布局。资源目录。这样,系统将相应地选择布局资源,如果设备是纵向或横向的。

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

答案 1 :(得分:1)

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

这是Android前端开发中一个完全常见的问题 - 您自己设计完美但面临某些尺寸/密度的技术问题。

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

  1. RadioButton在其大小减小时圈出作物。但它包含一些内置填充。您可以尝试更改每个RadioButton的高度。您只需确保其结果与所有密度和尺寸的屏幕兼容。
  2. 或者,您可以应用以下3个步骤:

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

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

    3)尽可能多地增加标题layout_weight

    上面的方法会减少标题大小并增加RadioGroup的大小,同时将RadioButton均匀地分布在里面。

    1. 通过裁剪不会立即在RadioGroup上方进行判断,这可能意味着您在标题上有padding_bottom。您应该从对话框标题中删除/减少padding_bottom。如果您需要在垂直模式下填充,您可以创建2个不同的布局,如this answer,或者通过onConfigurationChanged以编程方式处理,如所描述的here(在水平模式下删除padding_bottom,并在垂直模式下恢复它) )。

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