简体   繁体   English

android自定义对话框视图不适合对话框布局

[英]android custom dialog view not fit in the dialog layout

I create a alertdialog dynamically, it seems okay except that there are some space between the top of dialog view and the top of dialog layout margin. 我动态创建了一个Alertdialog,除了在对话框视图的顶部和对话框布局的边缘之间有一些空间之外,这似乎还可以。 Check the pic would be more clear. 检查图片会更清晰。

对话框布局边距问题

Is there some problem when I inflate the view? 给视图充气时有什么问题吗? I just google it, but I cannot find any clues. 我只是用谷歌搜索,但找不到任何线索。 Please help to modify the layout. 请帮助修改布局。

The dialog.xml is as following dialog.xml如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="vertical">
    <TextView
        android:id="@+id/dialog_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:textSize="20sp"
        android:background="@color/light_blue"
        android:gravity="center"
        android:paddingLeft="15sp"
        android:paddingRight="15sp"
        android:paddingTop="5sp"
        android:paddingBottom="5sp"
        android:textColor="@color/white"
        />
    <TextView
        android:id="@+id/dialog_msg"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/hello"
        android:textSize="17sp"
        android:paddingTop="15sp"
        android:paddingBottom="10sp"
        android:textColor="@color/black"
        />
    <Button
        android:id="@+id/dialog_button"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_centerInParent="true"
        android:text="@string/Ok"
        android:textColor="@color/light_blue"
        android:textSize="18sp"
        android:background="@color/white"
        />
</LinearLayout>

And the code I dynamic create dialog 我动态创建对话框的代码

private AlertDialog createDialog(String title, String msg){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    //AlertDialog dialog = builder.create();

    LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
        (Context.LAYOUT_INFLATER_SERVICE);
    View dialogView = inflater.inflate(R.layout.dialog, null);
    ((TextView)dialogView.findViewById(R.id.dialog_title)).setText(title);
    ((TextView)dialogView.findViewById(R.id.dialog_msg)).setText(msg);
    builder.setView(dialogView);
    final AlertDialog dialog = builder.create();
    ((Button)dialogView.findViewById(R.id.dialog_button)).setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            dialog.dismiss();
        }
    });
    return dialog;
}

Change your LinearLayout 's width and height from wrap_content to match_parent and try. 将您的LinearLayout的宽度和高度从wrap_content更改为match_parent并尝试。

Also try this 也试试这个

dialog.setView(dialogView,0,0,0,0); after calling 打电话后

final AlertDialog dialog = builder.create(); in your method 用你的方法

You have missed to show the bulider before return dialog. 您错过了在返回对话框之前显示bulider的信息。

builder.show(); builder.show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM