问题
I am creating a custom DialogFragment that is displayed underneath the actionbar. So far everything works great. The layout parameters for dialog fragment are match_parent
for width and wrap_content
for height.
I have tried every solution including setting the layout parameters .width and by getting Display size and removing the themes. However no matter what there is a small amount of space on left, right and top side of the dialog that is invisible. I need to know how to remove this space so it actually matches the width of the screen and hopefully gets rid of the top padding as well.
回答1:
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
回答2:
https://groups.google.com/forum/#!topic/android-developers/NDFo9pF8sHY
From Dianne Hackborn suggestion
Use non-dialog theme as android.R.style.Theme
or android.R.style.Theme_Light
.
Look @ the themes
https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml.
Check this link
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");
回答3:
if you set this theme to your dialog it will always be fullscreen
<!-- 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>
to do so you can use this setStyle(int,int) method.
dialogFragment.setStyle( DialogFragment.STYLE_NORMAL, R.style.You_Dialog );
回答4:
This worked for me with support for < API 11.
Create a style that is both full screen and has a transparent background:
<style name="TransparentDialog" parent="@android:style/Theme">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:background">@android:color/transparent</item>
</style>
Then your DialogFragment code:
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;
}
}
Finally, just for clarity, the contents of 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>
回答5:
I met the issue before: there is always a padding while having set fullscreen. try this code in dialogFragment's onActivityCreated() method:
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);
}
}
回答6:
First you need to know that handling of full screen in Dialog fragment is different from the normal Dialog component, second you need to customize the Dialog fragment before the actual creation of the dialog @ (OnCreateDialog), according to the answer of "user3244180" Full screen DialogFragment
@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;
}
回答7:
DialogFragment (when not told explicitly), will use its inner styles that will wrap your custom layout in it (no fullscreen, etc.).
No need to create custom theme for this problem. Just use this method and set style and theme of your choosing:
/* theme is optional, I am using leanback... */
setStyle(STYLE_NORMAL, R.style.AppTheme_Leanback);
回答8:
I was getting huge side padding in the DialogFragment for Nexus 6p and Pixel.
Fixed that by defining custom style as follows:
<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 did the trick
回答9:
For fullscreen dialog I have posted a answer
In this thread
Please check this is the most efficient and shortest way. Hope this helps :)
回答10:
<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);
}
回答11:
The following works perfectly for me. It lets me have a full-width dialog (fills the screen's width with no padding) but with wrap_content for height, and it retains all my other stylings that I do in my builder:
<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>
Background is required or else it does a weird repeat thing, but just set this to the color you want your dialog background to be. WindowBackground and WindowIsFloating are required to make the size wrap correctly.
Add your theme to your builder like so:
builder = new AlertDialog.Builder(_context, R.style.DialogTheme);
and you're good to go!
来源:https://stackoverflow.com/questions/18536439/dialogfragment-fullscreen-shows-padding-on-sides