android 自定义对话框视图不适合对话框布局
android custom dialog view not fit in the dialog layout
我动态创建了一个警告对话框,除了对话框视图顶部和对话框布局边距顶部之间有一些 space 之外,它看起来还不错。看图会更清楚
是不是膨胀视图有问题?我只是 google 它,但我找不到任何线索。请帮忙修改布局
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;
}
将 LinearLayout
的宽度和高度从 wrap_content
更改为 match_parent
并尝试。
也试试这个
dialog.setView(dialogView,0,0,0,0);
调用后
final AlertDialog dialog = builder.create();
在你的方法中
您错过了在 return 对话框之前显示构建器。
builder.show();
我动态创建了一个警告对话框,除了对话框视图顶部和对话框布局边距顶部之间有一些 space 之外,它看起来还不错。看图会更清楚
是不是膨胀视图有问题?我只是 google 它,但我找不到任何线索。请帮忙修改布局
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;
}
将 LinearLayout
的宽度和高度从 wrap_content
更改为 match_parent
并尝试。
也试试这个
dialog.setView(dialogView,0,0,0,0);
调用后
final AlertDialog dialog = builder.create();
在你的方法中
您错过了在 return 对话框之前显示构建器。
builder.show();