本文介绍了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上应用一个阴影使用这样的东西so答案:

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: http://stackoverflow.com/a/3723654/475217

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

10-29 13:12