Android:对话框在 droid 3 上有额外的、丑陋的空间? [英] Android: Dialogs have extra, ugly space on droid 3?

查看:17
本文介绍了Android:对话框在 droid 3 上有额外的、丑陋的空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一张图片来自 Galaxy Note,第二张来自 Droid 3.它们都由以下代码生成.

The first image is from a Galaxy Note, the second is from a Droid 3. Both of them produced from the below code.

Droid 3 上的对话框有大量额外的、难看的空间.这个空间在更复杂的对话框上更加丑陋.有什么办法可以预防吗?

The dialog on the Droid 3 has a significant amount of extra, ugly space. This space is even uglier on more complex dialogs. Is there any way to prevent it?

public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);

        TextView tv = new TextView(this);
        tv.setText("Hello!");

        Dialog dialog = new Dialog(this);
        dialog.setContentView(tv);
        dialog.setTitle("Hi!");

        dialog.show();
    }

推荐答案

这些 Droid UI 定制让我抓狂!

These Droid UI customizations drive me crazy!

如果您想控制您的对话框以使它们在设备之间保持一致,您可以使用提供样式参数的构造函数将它们简化为基础.以下示例给出了一个如下所示的对话框:

If you'd like to control your dialogs to make them consistent across devices, you can strip them down to the basics with a constructor that supplies a style parameter. The following example gives a dialog that looks like this:

首先,像这样实例化您的对话框(或者更好的是,创建一个扩展 Dialog 的自定义类,以便您可以重用它):

First, instantiate your dialog like this (or better yet, create a custom class that extends Dialog so you can reuse it):

Dialog dialog = new Dialog(context, android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.custom_dialog_layout);

然后,提供一个 custom_dialog_layout.xml(可能看起来像这样):

Then, supply a custom_dialog_layout.xml (could look something like this):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    style="@style/VerticalLinearLayout"
    android:background="@android:color/transparent"
    android:gravity="center_vertical|center_horizontal" >

    <LinearLayout
        android:id="@+id/dialog_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/dialog_background"
        android:orientation="vertical" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:textColor="@android:color/white"
            android:text="Hi!" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:textColor="@android:color/white"
            android:text="Hello!" />

    </LinearLayout>
</LinearLayout>

dialog_background.xml 看起来像这样:

where dialog_background.xml looks something like this:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <corners android:radius="10dp" />

    <stroke
        android:width="1dp"
        android:color="@android:color/black" />

    <gradient
        android:angle="0"
        android:startColor="@android:color/white"
        android:endColor="@android:color/white" />

</shape>

如果你真的想变得花哨,你可以尝试使用类似这样的答案在代码中对 dialog_layout 应用阴影:https://stackoverflow.com/a/372​​3654/475217

And if you really want to get fancy, you could try applying a drop shadow to the dialog_layout in code using something like this SO answer: https://stackoverflow.com/a/3723654/475217

这篇关于Android:对话框在 droid 3 上有额外的、丑陋的空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆