我正在使用警报框,它不适合我的设备屏幕。特别是宽度,我可以通过添加空白视图来增加高度,但是我不能增加宽度。它的左侧和右侧总是有一些空白空间。
我不知道它是android的默认属性还是可调整大小。我也尝试过自定义警报框,但对我不起作用。该警报框将更新进度并显示直到进度完成。
这是我的代码:
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.sync_dialog, null);
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(activity);
alertBuilder.setView(view);
alertBuilder.setCancelable(false);
AlertDialog progress = alertBuilder.create();
// Dialogs set their Window's top level layout width and height to WRAP_CONTENT.
// To make the Dialog bigger, you can set those parameters to FILL_PARENT.
progress.setCanceledOnTouchOutside(false);
progress.setCancelable(false);
progress.getWindow().getAttributes().width = LayoutParams.MATCH_PARENT;
progress.getWindow().getAttributes().height = LayoutParams.MATCH_PARENT;
mProgress = (ProgressBar) view.findViewById(R.id.progress);
/*Dialog dialog = new Dialog(activity, android.R.style.Theme_Translucent_NoTitleBar);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.sync_dialog);
dialog.setCanceledOnTouchOutside(false);
mProgress = (ProgressBar) view.findViewById(R.id.progress);*/
mText = (TextView) view.findViewById(R.id.message);
//Rebuilds ProgressBar with saved state when closed
if (!firstTime) {
mText.setText(mCurrentStatus);
mProgress.setProgress(mProgressStatus);
}
mProgress.setMax(mProgressMax);
return progress;
布局文件是:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/body"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="vertical"
android:paddingBottom="10dip"
android:paddingLeft="8dip"
android:paddingRight="8dip"
android:paddingTop="10dip" >
<ImageView
android:id = "@+id/btn_mfa_home"
android:src="@drawable/ic_mfa_icon_home"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
<ImageView
android:id = "@+id/btn_sync_home"
android:src="@drawable/btn_sync_home"
android:paddingTop="50dp"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="12dip"
android:text="@string/dialog_initial_syncing"
android:textColor="@android:color/black" />
<ProgressBar
android:id="@+id/progress"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_gravity="center_horizontal"
android:layout_width="240dip"
android:layout_height="wrap_content"
android:max="100" />
</LinearLayout>
任何建议将不胜感激!
您不是setting the attrbutes on the dialog
类似这样可能会有所帮助
AlertDialog.Builder adb = new AlertDialog.Builder(this);
Dialog d = adb.setView(new View(this)).create();
// (That new View is just there to have something inside the dialog that can grow big enough to cover the whole screen.)
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
d.show();
d.getWindow().setAttributes(lp);
请注意,在显示对话框之后设置属性。系统对设置它们的时间很挑剔。
尝试使用LayoutParams
mDialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
我帮助,请在yourAlertDialog.show()之后单独使用它>
alertDialog.show();
alertDialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);