Android获得自定义Dialog的全宽

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android获得自定义Dialog的全宽相关的知识,希望对你有一定的参考价值。

在我的应用程序中,我创建的自定义对话框没有完整的高度,我无法更改和自定义。例如看到这个屏幕截图:

我的代码:

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>
答案

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

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

替代方案(如果以上解决方案不起作用)

如果上面的代码不能正常工作,您可以使用解决方法

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);
另一答案
@Override public void onStart() 
    super.onStart();
    Dialog dialog = getDialog();
    if (dialog != null) 
        dialog.getWindow()
                .setLayout((int) (getScreenWidth(getActivity()) * .9), (int)(getScreenHeight(getActivity()) * .6) );
    


public static int getScreenWidth(Activity activity) 
    Point size = new Point();
    activity.getWindowManager().getDefaultDisplay().getSize(size);
    return size.x;


public static int getScreenHeight(Activity activity) 
    Point size = new Point();
    activity.getWindowManager().getDefaultDisplay().getSize(size);
    return size.y;

例如90%宽度和60%高度

另一答案
MyDialogFragment myDialogFragment = 
    new MyDialogFragment(activity,arraylist.get(position).getImage());
                                myDialogFragment.show();

Window window = myDialogFragment.getWindow();
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
另一答案

有两种方法可以完成,第一种在style.xml中,第二种在代码中:

  1. 在style.xml中添加如下,更改值(当前为90%)以满足您的需求。
<style name="Theme_Dialog" parent="android:Theme.Holo.Dialog">
    <item name="android:windowMinWidthMajor">90%</item>
    <item name="android:windowMinWidthMinor">90%</item>
</style>
  1. 将setlayout添加到match_parent
 final Dialog contacts_dialog = new Dialog(ActivityGroup.this,
 R.style.theme_sms_receive_dialog);
 contacts_dialog.setContentView(R.layout.dialog_schedule_date_time);
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);

 contacts_dialog.setCancelable(true);
 contacts_dialog.setCanceledOnTouchOutside(true);
 contacts_dialog.show();
另一答案

对于全角度对话框,您可以为对话框创建自定义样式。下面给出了全宽度对话框的代码:

<style name="Custom_Dialog" parent="ThemeOverlay.AppCompat.Light" >
    <item name="windowMinWidthMajor">100%</item>
    <item name="windowMinWidthMinor">65%</item>
</style>

要将此样式指定给对话框的构造函数,请在onCreate()之后立即将其添加到setContentView()方法:

getWindow()
    .setLayout(
        ViewGroup.LayoutParams.FILL_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT
    );
另一答案

您不需要添加任何样式,只需尝试下面的一个

  Dialog dialog = new Dialog(context);
  dialog.setContentView(R.layout.your_layout_here);
  dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  dialog.show();

注意:使用空的style在处理器内部耗费内存时间。而是直接使用dailog.getWindow().setLayout(width,height)

另一答案

如果有人想知道如何使用DialogFragment显示的对话框,你可以覆盖onCreateDialog返回一个自定义样式的对话框,其中windowMinWidthMajor和minor设置为90%

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) 
    return new Dialog(getActivity(), R.style.WideDialog);

样式:

<style name="WideDialog" parent="Base.Theme.AppCompat.Dialog">
    <item name="android:windowMinWidthMajor">90%</item>
    <item name="android:windowMinWidthMinor">90%</item>
</style>
另一答案

使用相对布局而不是线性布局来获得自定义对话框的全宽。

另一答案

在我的情况下,即使设置了width属性,随着对话框内的内容变小,自定义对话框的宽度也会缩小(宽度)

android:layout_width="match_parent"

我只是将宽度固定到屏幕尺寸,现在它根据我的要求工作

android:layout_width="320dp"
另一答案

要打开对话框时添加代码

       final Dialog mBottomSheetDialog = new Dialog(getActivity(), R.style.MaterialDialogSheet);

        mBottomSheetDialog.setContentView(R.layout.dialog_mainscreen_filter); // your custom view.
        mBottomSheetDialog.setCancelable(true);
        mBottomSheetDialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        mBottomSheetDialog.getWindow().setGravity(Gravity.BOTTOM);
        mBottomSheetDialog.show();


        ImageView img_cross = mBottomSheetDialog.findViewById(R.id.img_cross);
        final ImageView img_new = mBottomSheetDialog.findViewById(R.id.img_new);
        final ImageView img_Used = mBottomSheetDialog.findViewById(R.id.img_Used);


        img_cross.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                mBottomSheetDialog.dismiss();
            
        );


        img_new.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                img_new.setImageResource(R.drawable.checkbox_tick);
                img_Used.setImageResource(R.drawable.checkbox_tick_gray);
            
        );
        img_Used.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                img_Used.setImageResource(R.drawable.checkbox_tick);
                img_new.setImageResource(R.drawable.checkbox_tick_gray);
            
        );

对话框hml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >


    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@color/colorWhite"
        app:cardCornerRadius="5dp"
        app:cardElevation="@dimen/margin_10">

        <LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="@dimen/margin_10">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="@string/filter"
                    android:textStyle="bold"
                    android:textColor="@color/colorGreen"
                    android:textSize="@dimen/font_large" />

                <ImageView
                    android:id="@+id/img_cross"
                    android:layout_width="25dp"
                    android:layou

以上是关于Android获得自定义Dialog的全宽的主要内容,如果未能解决你的问题,请参考以下文章

android 如何让自定义dialog的宽度充满整个屏幕

datatables定义列宽自适应方法

【Android】自定义全屏dialog

如何自定义Android Dialog的样式?

移动模式我没有获得带有关闭按钮的全宽菜单

android自定义的dialog怎么设置view