Activity as dialog: Place in top left corner

2019-08-30 08:30发布

问题:

I'm trying to place an Android activity that uses the Holo.Dialog theme in the top left corner of my application by using the following code in OnCreate():

var layoutParams = this.Window.Attributes;
layoutParams.Gravity = GravityFlags.Top | GravityFlags.Left;

(This is Mono for Android)

It kind of works, however there is a tiny gap between the actual corner and the beginning of my dialog, which you can see in the following screen shot:

https://www.dropbox.com/s/cyy9lglq5642nz1/device-2013-05-26-223855.png

Notice the gap between the menu box and the actual edge of the screen. What can I do to remove that gap completly?

回答1:

Turns out the problem is rather simple: Theme.Holo.Dialog defines a background that adds a transparent border around the dialog. This causes the spacing between the corner and the dialog.

Creating a custom style fixes it:

<style name="MyCustomDialog" parent="android:Theme.Holo.Dialog">
    <item name="android:windowBackground">@null</item>
</style>

This overwrites the background and removes the spacing.