如何在Android中将弹出窗口修复为多个屏幕尺寸

时间:2016-04-04 11:29:27

标签: android popup

我在按钮点击事件的屏幕底部打开弹出窗口。弹出窗口在屏幕尺寸为720 x 1280像素(~294 ppi像素密度)的底部位置正确打开但是相同在屏幕尺寸为480 x 854像素(~196 ppi像素密度)下运行,它显示在屏幕中央。如何修复多个屏幕大小的弹出窗口问题。

这是我的弹出窗口xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup_element"
    android:layout_width="fill_parent"
    android:layout_height="180dp"
    android:background="@android:color/transparent"
    android:orientation="vertical"
    android:padding="3dp">


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="1dp"
        android:background="@android:color/transparent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/Title"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:background="@drawable/curve_shap"
            android:gravity="center_horizontal|center_vertical"
            android:text="Take Photo"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@android:color/darker_gray"
            android:textSize="14sp" />

        <Button
            android:id="@+id/button_Camera"
            android:layout_width="fill_parent"
            android:layout_height="40dp"

            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:background="@drawable/curve_shap"
            android:text="Camera"
            android:textColor="#3A86CF" />

        <Button
            android:id="@+id/button_Gallery"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:background="@drawable/curve_shap"
            android:text="Gallery"
            android:textColor="#3A86CF" />


    </LinearLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:layout_marginTop="4dp"
        >

        <Button
            android:id="@+id/btnCancelCamera"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_gravity="center_horizontal"

            android:background="@drawable/curve_shap"
            android:text="Cancel"
            android:textColor="#3A86CF"
            android:textSize="16sp"
            android:textStyle="bold" />
    </RelativeLayout>


</LinearLayout>

这是我的弹出窗口代码。

imgProfilePic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
                View popupView = layoutInflater.inflate(R.layout.camera_popup, (ViewGroup) findViewById(R.id.popup_element));
                popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.setWidth(720);
                popupWindow.setHeight(350);
                popupWindow.setFocusable(true);
                popupWindow.showAtLocation(popupView, Gravity.BOTTOM, 0, 0); //87
                popupWindow.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

                Button btnCamera = (Button) popupView.findViewById(R.id.button_Camera);
                Button btnGallery = (Button) popupView.findViewById(R.id.button_Gallery);
                Button btnDismiss = (Button) popupView.findViewById(R.id.btnCancelCamera);

                btnCamera.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        SimpleDateFormat s = new SimpleDateFormat("ddMMyyyyhhmmss");
                        String picformat = "IMG_" + 0 + "_" + s.format(new Date()) + ".png";
                        Log.e(" picformat ", " = " + picformat);

                        Intent i = new Intent(Filter_Screen.this , Image_Cropping.class );
                        startActivity(i);

                        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        String extr = Environment.getExternalStorageDirectory().toString() + File.separator + "classNKK_ProfilePic";
                        File myPath = new File(extr, picformat);
                        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(myPath));
                        startActivityForResult(cameraIntent, CAMERA_REQUEST);

                        Log.e("Camera", " Open");
                        String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
                        String imageUserProfile_path = baseDir + "/classNKK_ProfilePic/" + picformat;
                        editor.putString("profile_picformat", imageUserProfile_path);
                        editor.commit();


                        popupWindow.dismiss();


                    }
                });

                btnGallery.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(galleryIntent, PROFILE_GALLERY);
                        Log.e("Gallery open", "");
                    }
                });

                btnDismiss.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                    }
                });
            }
        });

我必须为多个屏幕大小创建布局文件夹,然后将弹出窗口xml文件放在此文件夹中。

  1. 布局
  2. 布局HDPI
  3. layout-mdpi
  4. 先谢谢了。

1 个答案:

答案 0 :(得分:0)

您应该考虑到设备具有不同的屏幕尺寸:

  • xlarge屏幕至少为960dp x 720dp
  • 大屏幕至少为640dp x 480dp
  • 正常屏幕至少为470dp x 320dp
  • 小屏幕至少为426dp x 320dp

因此,我建议您不要将布局放在layout-hdpi和layout-mdpi中,而是建议为每个屏幕尺寸创建一个dimens.xml,并在那里提取与尺寸相关的值。

例如,如果您希望普通屏幕的弹出高度为100dp,大屏幕则为200dp,请创建以下文件:

RES /值正常/ dimens.xml

<resources>
    <dimen name="popup_height">100dp</dimen>
</resources>

RES /值-大/ dimens.xml

<resources>
    <dimen name="popup_height">200dp</dimen>
</resources>

并在您的布局中使用

android:layout_height="@dimen/popup_height"

可以找到其他信息here