32

我正在创建一个显示在操作栏下方的自定义 DialogFragment。到目前为止,一切都很好。对话框片段的布局参数是match_parent宽度和wrap_content高度。

我已经尝试了所有解决方案,包括设置布局参数 .width 以及获取显示大小和删除主题。但是,无论如何,对话框的左侧、右侧和顶部都有少量空间是不可见的。我需要知道如何删除这个空间,以便它实际上与屏幕的宽度相匹配,并希望也能摆脱顶部填充。

4

11 回答 11

34

I figured it out using custom dialog theme. windowIsFloating true will get rid of the background but will add some extra space underneath the background as a background. In which case you can use windowBackground @null to erase it.

<style name="CustomDialog" parent="@android:style/Theme.Holo.Light" >
    <item name="android:windowBackground">@null</item>
    <item name="android:windowIsFloating">true</item>
</style>

Usage:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.CustomDialog);

Thank you to Raghunandan who gave me the link that includes all style attributes. It took me a while but I went through that file and found very interesting elements. Definitely have a look at the link posted below to explore theme styles.

https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml

于 2013-09-01T03:14:52.083 回答
15

https://groups.google.com/forum/#!topic/android-developers/NDFo9pF8sHY

来自 Dianne Hackborn 的建议

使用非对话主题作为android.R.style.Themeor android.R.style.Theme_Light

看@主题

https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml

检查此链接

http://developer.android.com/reference/android/app/DialogFragment.html

DialogFragment picker = MyDialogFragment.newInstance();
picker.setStyle( DialogFragment.STYLE_NORMAL, android.R.style.Theme );
picker.show(getFragmentManager(), "MyDialogFragment");
于 2013-08-30T15:56:36.543 回答
9

如果您将此主题设置为对话框,它将始终全屏

<!-- DIALOG STYLE -->
<style name="You.Dialog" parent="android:Theme.Holo.Dialog" >
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowIsFloating">false</item>
</style>

为此,您可以使用此setStyle(int,int)方法。

dialogFragment.setStyle( DialogFragment.STYLE_NORMAL, R.style.You_Dialog );
于 2013-08-30T16:08:05.280 回答
7

首先要知道Dialog fragment中全屏的处理与普通Dialog组件不同,其次需要在实际创建对话框@(OnCreateDialog)之前自定义Dialog fragment,根据“user3244180”的回答完整屏幕对话框片段

 @Override
 public Dialog onCreateDialog(final Bundle savedInstanceState) {

     // the content
     final RelativeLayout root = new RelativeLayout(getActivity());
     root.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

     // creating the fullscreen dialog
     final Dialog dialog = new Dialog(getActivity());
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
     dialog.setContentView(root);
     dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
     dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

     return dialog;
 }
于 2014-09-02T12:02:55.220 回答
7

这对我有用,支持 < API 11。

创建一个既全屏又具有透明背景的样式:

<style name="TransparentDialog" parent="@android:style/Theme">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:background">@android:color/transparent</item>
</style>

然后你的 DialogFragment 代码:

public class MyDialog extends DialogFragment {

     public static MyDialog newInstance() {
         MyDialog dialog = new MyDialog();
         dialog.setStyle(DialogFragment.STYLE_NO_FRAME, R.style.TransparentDialog);
         return dialog;
     }

     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.my_custom_layout, container, false);
        return view;
    }
}

最后,为了清楚起见,my_custom_layout.xml 的内容:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.kabx"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/semi_transparent_grey" >

..........................
..Whatever You Want Here..
..........................

</RelativeLayout>
于 2014-09-17T10:14:06.250 回答
6

我之前遇到过这个问题:设置全屏时总是有填充。在 dialogFragment 的 onActivityCreated() 方法中尝试以下代码:

public void onActivityCreated(Bundle savedInstanceState)
{   
    Window window = getDialog().getWindow();
    LayoutParams attributes = window.getAttributes();
    //must setBackgroundDrawable(TRANSPARENT) in onActivityCreated()
    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    if (needFullScreen)
    {
        window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    }
}
于 2013-11-21T08:55:33.910 回答
2
<style name="WideDialog" parent="Theme.AppCompat.Light.DialogWhenLarge">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">false</item>
        <item name="android:windowIsFloating">false</item>
    </style>

@Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.WideDialog);
    }
于 2018-06-26T18:47:33.893 回答
1

DialogFragment(当没有明确告知时)将使用其内部样式,将您的自定义布局包装在其中(无全屏等)。

无需为此问题创建自定义主题。只需使用此方法并设置您选择的样式和主题:

/* theme is optional, I am using leanback... */
setStyle(STYLE_NORMAL, R.style.AppTheme_Leanback);
于 2016-09-01T18:05:50.313 回答
1

以下内容非常适合我。它让我有一个全角对话框(填充屏幕宽度,没有填充),但高度为 wrap_content,它保留了我在构建器中所做的所有其他样式:

<style name="DialogTheme">

    <item name="windowMinWidthMajor">100%</item>
    <item name="windowMinWidthMinor">100%</item>

    <item name="android:windowBackground">@null</item>
    <item name="android:windowIsFloating">true</item>

    <item name="android:background">#ffffff</item>

</style>

背景是必需的,否则它会重复发生奇怪的事情,但只需将其设置为您希望对话框背景的颜色即可。需要 WindowBackground 和 WindowIsFloating 才能使尺寸正确换行。

像这样将您的主题添加到您的构建器中:

builder = new AlertDialog.Builder(_context, R.style.DialogTheme);

你可以走了!

于 2018-08-13T22:39:12.650 回答
0

对于全屏对话框,我发布了一个答案

在这个线程中

请检查这是最有效和最短的方法。希望这可以帮助 :)

于 2017-06-26T11:01:37.343 回答
0

我在 Nexus 6p 和 Pixel 的 DialogFragment 中获得了巨大的侧边距。

通过定义自定义样式来修复该问题,如下所示:

<style name="Custom.Dialog" parent="android:Theme.DeviceDefault.Dialog.NoActionBar.MinWidth" >
        <item name="android:windowBackground">@null</item>
        <item name="android:windowIsFloating">true</item>
</style>

parent="android:Theme.DeviceDefault.Dialog.NoActionBar.MinWidth成功了

于 2017-03-23T04:00:37.107 回答