如何使用AlertDialog使用屏幕的完整宽度

时间:2015-04-29 07:59:16

标签: android xml alertdialog

我想使用整个屏幕宽度来显示对话框和图像...

我已编写自定义xml文件以显示警告对话框,我已发布完整代码,用于将SDCard中的图像显示到对话框中。

代码:

holder.viewImageButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View vi) {      

        final AlertDialog.Builder imageDialog = new AlertDialog.Builder(UploadActivity.this);
        final LayoutInflater inflater = (LayoutInflater)                            getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);

        View layout = inflater.inflate(R.layout.custom_fullimage_dialog,
         (ViewGroup) findViewById(R.id.layout_root));
         ImageView image = (ImageView) layout.findViewById(R.id.fullimage);

        try
        {
           fileName = ImageList.get(position).toString().
            substring(strPath.lastIndexOf('/')+1, strPath.length());

            String fileToView = Environment.getExternalStorageDirectory().getPath() + "/Pictures/Joseph/" + CameraLauncherActivity.folder+ "/" + fileName;
            Log.d("FileToDelete", fileToView);

            // to get Image
            newFile = new File(fileToView); // get the related file on click
            // to set Image
            image.setImageURI(Uri.fromFile(newFile));       

            } catch (Exception e) { 
                // When Error
                e.printStackTrace();
            image.setImageResource(R.drawable.fail);
        }                      

        // to get Name
        fileName=ImageList.get(position).toString().substring
            (strPath.lastIndexOf('_')+1, strPath.length());
        imageDialog.setTitle(fileName);
        imageDialog.setView(layout);
        imageDialog.setPositiveButton(android.R.string.ok, new 
            DialogInterface.OnClickListener(){

        public void onClick(DialogInterface dialog, int which) {
        // to dismiss the dialog
                dialog.dismiss();
            }                    
        });     

        imageDialog.create();
        imageDialog.show();
       }
  });

custom_fullimage_dialog.xml: -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="3"
        android:gravity="center">

    <ImageView
        android:id="@+id/fullimage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@null" />

    <TextView
        android:id="@+id/custom_fullimage_placename"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textColor="#FFF" >
    </TextView>

</LinearLayout>

3 个答案:

答案 0 :(得分:1)

您可以使用WindowManager设置属性。你可以这样试试:

    Dialog dialog = new Dialog(getActivity());
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.dialog_custom);
    dialog.getWindow().setBackgroundDrawable(getResources().getDrawable(R.drawable.background)); // Optional Background
    dialog.setCanceledOnTouchOutside(true);
    dialog.setCancelable(true);
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(dialog.getWindow().getAttributes());
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.MATCH_PARENT;

        // DO STUFF
        // dialog.findViewById(.....)

    dialog.show();
    dialog.getWindow().setAttributes(lp);

答案 1 :(得分:1)

我已使用以下代码行解决了这个问题:

image.setScaleType(ImageView.ScaleType.FIT_XY);

答案 2 :(得分:0)

您还可以使用DialogActivity全屏显示。使用简单的Activity显示imageview并将其主题设置为Dialog。

<style name="ThemeDialog" parent="AppBaseTheme">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:backgroundDimEnabled">true</item>
</style>

现在点击按钮的startActivity和Android风格中的ActivityManifest文件集主题。

 <activity
            android:name=".ActivityName"
            android:theme="@style/ThemeDialog" />