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

时间:2016-07-27 08:40:17

标签: android android-layout alertdialog

我动态创建一个alertdialog,除了在对话框视图顶部和对话框布局边缘顶部之间有一些空间外,似乎还可以。检查图片会更清楚。

dialog layout margin problem

当我给视图充气时有问题吗?我只是谷歌,但我找不到任何线索。请帮助修改布局。

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>

代码我动态创建对话框

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;
}

2 个答案:

答案 0 :(得分:1)

LinearLayout的宽度和高度从wrap_content更改为match_parent,然后尝试。

也试试这个

致电

dialog.setView(dialogView,0,0,0,0); 您的方法中

final AlertDialog dialog = builder.create();

答案 1 :(得分:0)

你错过了在返回对话框之前显示浮标。

builder.show();