Android会在全屏显示对话框

Mih*_*scu 22 android android-dialog android-fullscreen

我需要对话框来填充屏幕,除了顶部和底部的一些空间.我正在寻找一个解决方案,但找不到一个可能是因为我正在宣布它onClickListener.有人可以给一个解决方案吗?

活动代码:

sort.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                AlertDialog.Builder sort = new AlertDialog.Builder(HomeScreen.this);
                // Get the layout inflater
                LayoutInflater inflater = HomeScreen.this.getLayoutInflater();
                View sortView = inflater.inflate(R.layout.sort_layout, null);
                sort.setView(sortView);
                sort.create().show();
            }
        });
Run Code Online (Sandbox Code Playgroud)

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginBottom="50dp"
    android:layout_marginTop="50dp"
    android:background="@color/white"
    android:orientation="vertical" android:layout_gravity="center">
    + some more stuff here I dont think it's relevant
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

kal*_*pvs 40

这里将屏幕宽度和宽度设置为对话框

WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(dialog.getWindow().getAttributes());
    lp.width = width;
    lp.height = height;
    dialog.getWindow().setAttributes(lp);
Run Code Online (Sandbox Code Playgroud)

或者在你的风格中创建一个这样的风格

 <style name="DialogTheme" parent="android:Theme.Dialog">

    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">fill_parent</item>


    <!-- No backgrounds, titles or window float -->
    <item name="android:windowBackground">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">false</item>
</style>
Run Code Online (Sandbox Code Playgroud)

像这样创建一个对话框对象

dialog = new Dialog(this, R.style.DialogTheme);
Run Code Online (Sandbox Code Playgroud)

编辑::

获得它将填充的任何设备的宽度和高度

WindowManager manager = (WindowManager) getSystemService(Activity.WINDOW_SERVICE);
    int width, height;
    LayoutParams params;

    if (Build.VERSION.SDK_INT > VERSION_CODES.FROYO) {
        width = manager.getDefaultDisplay().getWidth();
        height = manager.getDefaultDisplay().getHeight();
    } else {
        Point point = new Point();
        manager.getDefaultDisplay().getSize(point);
        width = point.x;
        height = point.y;
    }
Run Code Online (Sandbox Code Playgroud)

  • 设置 `&lt;item name="android:windowBackground"&gt;​​@null&lt;/item&gt;` 不会为您提供透明背景,但是 `&lt;item name="android:windowBackground"&gt;​​@android:color/transparent&lt;/item&gt; ` 会的。 (2认同)

Piy*_*ush 11

首先在style.xml文件中为对话框创建自定义样式:

<style name="full_screen_dialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:windowBackground">@android:color/transparent</item>
</style>
Run Code Online (Sandbox Code Playgroud)

然后将此主题设置为警报对话框:

 AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this,  R.style.full_screen_dialog));
Run Code Online (Sandbox Code Playgroud)

或者您可以调用新活动并将此自定义样式作为清单文件中的主题提供给该活动...