Android - 自定义对话框太大

时间:2014-04-08 14:31:00

标签: android alertdialog layoutparams

我在定义自己的警报式对话框时遇到了一些严重的问题,包括性感的颜色和徽标。这一切都很好,我几乎就在那里,只是一个小问题。

当有大量文字时,对话框会填满整个屏幕。查看正常的警报对话框,当消息很长时,滚动视图占用的最大宽度和高度都很好。

有人能告诉我如何使用此设置模拟传统警报对话框的尺寸吗?

从我看过的各种线程中,这是我的解决方案(对代码的长度道歉,我猜最重要的部分是xml中的scrollview):

MyDialog.java

public class MyDialog extends Dialog {

    public MyDialog(Context context, int theme) {
        super(context, theme);
        // TODO Auto-generated constructor stub
    }

}

MainActivity

MyDialog dialog = new MyDialog(getActivity(), R.style.greenDialog);
dialog.setContentView(R.layout.dialog_about);
TextView titleTextView = (TextView) dialog.findViewById(R.id.alertTitle);
titleTextView.setText(title);

TextView text = (TextView) dialog.findViewById(R.id.message);
text.setText("Blah blah blah" +
        "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris mollis tristique egestas. Aliquam rutrum auctor mi, ac convallis quam accumsan a. In hac habitasse platea dictumst. Mauris molestie mauris libero, sit amet tincidunt risus scelerisque eget. Morbi eu semper odio. Phasellus sollicitudin elementum lorem. Phasellus vel mauris augue. Quisque hendrerit adipiscing elementum. Integer leo mi, placerat et sodales sit amet, pretium id diam. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec id vehicula lacus.\n" +
        "\n" +
        "Phasellus enim leo, aliquam eu ante eu, accumsan lacinia enim. Etiam vitae sapien ullamcorper felis faucibus accumsan eu eget nibh. Integer tristique, metus sit amet ornare pretium, justo nunc consequat mi, ac ultricies lectus massa quis mauris. Vivamus lectus elit, condimentum vitae tincidunt at, feugiat sed quam. Mauris sollicitudin lacus ut diam rhoncus blandit. Phasellus ac lacus risus. Praesent convallis, libero non suscipit porttitor, lectus lacus congue tellus, vitae pulvinar quam dui sit amet enim.\n" +
        "\n" +
        "Quisque vehicula, elit ut mattis tempor, urna nisi ullamcorper odio, vitae commodo velit ante tincidunt urna. Duis condimentum sed nibh in faucibus. Suspendisse est felis, vestibulum sed volutpat volutpat, mattis eu sem. Morbi fermentum lorem a neque tristique, auctor interdum risus tempus. Morbi tempor nibh lorem, id accumsan lectus imperdiet sed. Nulla et sollicitudin eros, mattis dignissim lacus. Nam pulvinar magna non justo lobortis viverra. Etiam quis venenatis justo. Quisque non libero vitae magna ullamcorper laoreet. Praesent dignissim erat sit amet mi mattis sagittis. Morbi quis neque feugiat nulla fringilla sagittis. Mauris dui arcu, feugiat in dolor sed, accumsan placerat ligula.\n" +
        "\n" +
        "Suspendisse tempor enim et augue scelerisque, non rhoncus massa ultricies. Phasellus malesuada tempus ultrices. Proin dapibus sagittis orci sed ullamcorper. Phasellus lacus sem, auctor ut tincidunt at, suscipit ultricies ipsum. Nunc eget lectus elit. Aenean posuere volutpat purus, et accumsan nibh malesuada a. Vivamus egestas sed felis sed viverra. Pellentesque id elit ut leo rhoncus fermentum. Nullam id purus odio. Sed eu sapien vitae ante scelerisque tempus sed sit amet nisl. Vivamus nec orci suscipit, interdum libero quis, fringilla massa.\n" +
        "\n" +
        "Nunc laoreet condimentum fringilla. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam facilisis vulputate sapien, ac eleifend augue. Sed eget justo gravida, volutpat risus quis, sodales est. Integer blandit a ligula in molestie. Sed et turpis gravida, placerat eros ac, porta nisi. Quisque in elit a dui malesuada lobortis. Quisque pretium justo vel justo mollis, eget cursus metus condimentum. Praesent ultrices ante libero, sit amet molestie orci dictum sed. Suspendisse elementum a felis at malesuada. Sed tempus nulla sit amet enim pulvinar, vel elementum ante imperdiet. Proin vitae ligula pellentesque, aliquam lectus ac, luctus ipsum.");

Button btn = (Button)dialog.findViewById(R.id.button1);
btn.setText("OK");

dialog.show();

dialog_about.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parentPanel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingTop="9dip"
    android:paddingBottom="3dip"
    android:paddingStart="3dip"
    android:paddingEnd="1dip">

    <LinearLayout android:id="@+id/topPanel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="54dip"
        android:orientation="vertical">
        <LinearLayout android:id="@+id/title_template"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="6dip"
            android:layout_marginBottom="9dip"
            android:layout_marginStart="10dip"
            android:layout_marginEnd="10dip">
            <ImageView android:id="@+id/icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="top"
                android:paddingTop="6dip"
                android:paddingEnd="10dip"
                android:src="@drawable/ic_launcher" />
            <TextView
                android:id="@+id/alertTitle"
                style="?android:attr/textAppearanceLarge"
                android:layout_gravity="center_vertical"
                android:singleLine="true"
                android:ellipsize="end"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
        <ImageView android:id="@+id/titleDivider"
            android:layout_width="match_parent"
            android:layout_height="9dp"
            android:src="@drawable/horizontal_divider" />
        <!-- If the client uses a customTitle, it will be added here. -->
    </LinearLayout>

    <LinearLayout android:id="@+id/contentPanel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">
        <ScrollView android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="2dip"
            android:paddingBottom="12dip"
            android:paddingStart="14dip"
            android:paddingEnd="10dip"
            android:overScrollMode="ifContentScrolls">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="5dip">

                <TextView android:id="@+id/message"
                    style="?android:attr/textAppearanceMedium"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <ImageView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:src="@drawable/img_view_logo_small_splash"
                        android:scaleType="centerInside"
                        android:layout_gravity="end"/>

                    <TextView android:id="@+id/legal"
                        style="?android:attr/textAppearanceSmall"
                        android:text="\u00a9 My Company Ltd. info"
                        android:layout_weight="1"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"/>
                </LinearLayout>
            </LinearLayout>
        </ScrollView>
    </LinearLayout>

    <LinearLayout android:id="@+id/buttonPanel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="54dp"
        android:orientation="vertical" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
            <Button android:id="@+id/button1"
                android:layout_width="0dip"
                android:layout_gravity="start"
                android:layout_weight="1"
                android:textColor="@color/green_lightest"
                android:background="@drawable/selector_transparent_button"
                android:maxLines="2"
                android:layout_height="match_parent" />
            <Button android:id="@+id/button2"
                android:layout_width="0dip"
                android:layout_gravity="end"
                android:layout_weight="1"
                android:background="@drawable/selector_transparent_button"
                android:maxLines="2"
                android:layout_height="wrap_content"
                android:visibility="gone"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

这非常接近android的alert_dialog.xml另外需要注意的是样式使用无标题功能,否则我会得到奇怪的空填充:

styles.xml

<style name="greenDialog" parent="android:Theme.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@color/green_shadow</item>
</style>

警告对话框的屏幕截图:

Alert Dialog Problem

编辑:为了比较,这里是一个具有相同文本的正常警告对话框:

Alert Dialog Normal

1 个答案:

答案 0 :(得分:1)

dialog_about.xml中,将LinearLayout android:id="@+id/parentPanel"高度设置为所需大小而不是android:layout_height="wrap_content",并在LinearLayout android:id="@+id/parentPanel"内使用scrollview,它将正常工作......