简体   繁体   English

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

[英]How to fix Pop up window to multiple screen size in Android

I'm creating popup window open at the bottom of screen on button click event.The popup window is correctly open at the location of bottom in screen size of 720 x 1280 pixels (~294 ppi pixel density) but same as to run in the screen size of 480 x 854 pixels (~196 ppi pixel density) it is displaying in the center of screen. 我正在创建单击按钮事件在屏幕底部打开的弹出窗口。弹出窗口在屏幕尺寸为720 x 1280像素(〜294 ppi像素密度)的底部位置正确打开,但与在屏幕尺寸为480 x 854像素(〜196 ppi像素密度),显示在屏幕中央。 How to fix this popup window issue for multiple screen size. 如何解决多个屏幕尺寸的弹出窗口问题。

This is my popup xml 这是我的弹出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>

This is my popup window code. 这是我的弹出窗口代码。

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

And i have to create folder of layout for multiple screen size and i put the popup window xml file in this folders. 而且我必须为多个屏幕尺寸创建布局文件夹,然后将弹出窗口xml文件放在此文件夹中。

  1. layout 布局
  2. layout-hdpi 布局-hdpi
  3. layout-mdpi 布局-mdpi

Thanks in advanced . 提前致谢 。

You should take into consideration that devices have different screen sizes: 您应考虑到设备的屏幕大小不同:

  • xlarge screens are at least 960dp x 720dp xlarge屏幕至少为960dp x 720dp
  • large screens are at least 640dp x 480dp 大屏幕至少为640dp x 480dp
  • normal screens are at least 470dp x 320dp 普通屏幕至少为470dp x 320dp
  • small screens are at least 426dp x 320dp 小萤幕至少为426dp x 320dp

So instead of placing your layout in layout-hdpi and layout-mdpi, I would suggest creating a dimens.xml for each screen size and extract your size related values there. 因此,建议您不要为每个屏幕尺寸创建一个dimens.xml并在其中提取与尺寸相关的值,而不是将布局放置在layout-hdpi和layout-mdpi中。

For example if you want the popup height to be 100dp for normal screens and 200dp for large screens create the following files: 例如,如果您希望正常屏幕的弹出高度为100dp,大屏幕的弹出高度为200dp,请创建以下文件:

res/values-normal/dimens.xml res / values-normal / dimens.xml

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

res/values-large/dimens.xml res / values-large / dimens.xml

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

and in your layout use 并在您的布局中使用

android:layout_height="@dimen/popup_height"

Additional info can be found here . 其他信息可以在这里找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM