AlertDialog - gap between custom view and title/bu

2019-09-16 03:09发布

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?

enter image description here

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条回答
该账号已被封号
2楼-- · 2019-09-16 03:43

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.

Dialog with no bottom gap

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>

Dialog with no gaps

查看更多
登录 后发表回答