繁体   English   中英

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

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

我正在创建单击按钮事件在屏幕底部打开的弹出窗口。弹出窗口在屏幕尺寸为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. 布局-mdpi

提前致谢 。

您应考虑到设备的屏幕大小不同:

  • xlarge屏幕至少为960dp x 720dp
  • 大屏幕至少为640dp x 480dp
  • 普通屏幕至少为470dp x 320dp
  • 小萤幕至少为426dp x 320dp

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

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

res / values-normal / dimens.xml

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

res / values-large / dimens.xml

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

并在您的布局中使用

android:layout_height="@dimen/popup_height"

其他信息可以在这里找到。

暂无
暂无

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

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