Android获得自定义对话框的全宽 [英] Android get full width for custom Dialog

查看:64
本文介绍了Android获得自定义对话框的全宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我创建的自定义对话框没有完整的高度,我无法更改和自定义它.例如,请参见以下屏幕截图:

in my application my created custom dialog dont have full height and i can not change and customize that.for example see this screen shot:

我的代码:

final Dialog contacts_dialog = new Dialog(ActivityGroup.this, R.style.theme_sms_receive_dialog);
contacts_dialog.setContentView(R.layout.dialog_schedule_date_time);
contacts_dialog.setCancelable(true);
contacts_dialog.setCanceledOnTouchOutside(true);
contacts_dialog.show();

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutRoot"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@null"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_header_dialog_background"
        android:orientation="horizontal"
        android:padding="4dp" >

        <TextView
            android:id="@+id/TextView21"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginRight="5dp"
            android:layout_weight="2"
            android:gravity="center_vertical|right"
            android:text="@string/choose_schedule_time_date"
            android:textColor="#ffffff"
            android:textSize="14sp"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/ImageView03"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginTop="0dp"
            android:background="@drawable/icon_scudule_time" />
    </LinearLayout>

</LinearLayout>

样式:

<style name="theme_sms_receive_dialog" parent="android:style/Theme.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:background">@android:color/transparent</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="numberPickerStyle">@style/NPWidget.Holo.Light.NumberPicker</item>
</style>

推荐答案

您需要获取当前Window并将其设置为LayoutParams,如下所示:

You need to get the current Window and set it's LayoutParams like so:

Dialog d=new Dialog(mContext);
.........
.........
myDialog.show();
Window window = myDialog.getWindow();
window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

替代方法(如果上述解决方案无效)

如果上述代码无法正常运行,则可以使用变通办法

In case above code not works well you can use a workaround

styles.xml

<style name="mydialog"></style>

Java

Dialog d=new Dialog(mContext,R.style.mydialog);
.........
.........
myDialog.show();
Window window = myDialog.getWindow();
window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

这篇关于Android获得自定义对话框的全宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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