问题
I use a custom view for an AlertDialog. In the emulator, there is some space above and below the customeView. If I set a title and buttons for the AlertDialog, I can see the gap between the title panel/button panel and the customView. How can I remove the gap?
My code to open the AlertDialog:
btnShowDialog.Click += (object sender, EventArgs e) =>
{
var customView = LayoutInflater.Inflate(Resource.Layout.myDialog, null);
var builder = new AlertDialog.Builder(this);
builder.SetTitle("Title");
builder.SetView(customView);
builder.Create().Show();
};
The customView layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF">
<TextView
android:id="@+id/DialogText"
android:text="This is my custom view. Blur Blur Blur"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<EditText
android:id="@+id/EnterPhone"
android:inputType="phone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:hint="enter your number" />
</LinearLayout>
I have tested the AlertDialog in android 4.0.3 and 4.1.2 emulator and get the same issue.
Thanks for help!
回答1:
Instead of using AlertDialog try using Dialog instead like so:
var dialog = new Dialog(this);
dialog.SetTitle("Hey there");
dialog.SetContentView(Resource.Layout.myDialog);
dialog.Show();
This gives me the a dialog with no gap in the bottom.
You could also leave the title out and fill the entire Dialog with your custom view.
var dialog = new Dialog(this);
dialog.RequestWindowFeature((int)WindowFeatures.NoTitle);
dialog.SetContentView(Resource.Layout.myDialog);
dialog.Show();
And use this layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000">
<TextView
android:text="Hey there"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
style="@android:style/TextAppearance.DialogWindowTitle" />
</LinearLayout>
<TextView
android:id="@+id/DialogText"
android:text="This is my custom view. Blur Blur Blur"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<EditText
android:id="@+id/EnterPhone"
android:inputType="phone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:hint="enter your number" />
</LinearLayout>
来源:https://stackoverflow.com/questions/15305791/alertdialog-gap-between-custom-view-and-title-buttons