如何安卓弹出窗口匹配屏幕大小宽度

时间:2017-04-26 14:51:15

标签: android popup

我在Activity中实现了弹出窗口。问题是我想要弹出窗口的设置宽度匹配屏幕大小,现在我在两侧都留有空间。现在看来它是Popup picture sample

的样子

这是我的弹出代码:

                    LayoutInflater inflater = (LayoutInflater) MainActivity.this
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    //Inflate the view from a predefined XML layout
                    View layout = inflater.inflate(R.layout.activity_pop_up, null);

                    ImageView baner3img = (ImageView ) layout.findViewById(R.id.baner3img);

                    Picasso.with(MainActivity.this).load(hrefScroll.get(2))
                            .into(baner3img);
                    // create  PopupWindow
                    pw = new PopupWindow(layout, ViewPager.LayoutParams.MATCH_PARENT, ViewPager.LayoutParams.MATCH_PARENT,true);

                    ImageView close = (ImageView) layout.findViewById(R.id.close);

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

                            findViewById(R.id.relativeLayoutZaFragment).post(new Runnable() {
                                public void run() {
                                    pw.dismiss();
                                }
                            });
                        }
                    });

                    findViewById(R.id.relativeLayoutZaFragment).post(new Runnable() {
                        public void run() {
                            pw.setWindowLayoutMode(
                                   ViewGroup.LayoutParams.MATCH_PARENT,
                                   ViewGroup.LayoutParams.MATCH_PARENT);
                            pw.setHeight(1);
                            pw.setWidth(1);
                            pw.showAtLocation(findViewById(R.id.relativeLayoutZaFragment), Gravity.CENTER, 1, 1);
                            new Handler().postDelayed(new Runnable(){
                                public void run() {
                                    pw.dismiss();
                                }
                            }, 5 *1000);
                        }
                    });
                }
            });

activity_pop_up.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/popup"
android:layout_width="match_parent"

android:layout_height="match_parent"
tools:context="com.example.bolt.automagazin.PopUp">


<ImageView
    android:id="@+id/baner3img"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_alignParentStart="true"
    android:layout_centerVertical="true"
    app:srcCompat="@drawable/autojedan" />

<ImageView
    android:id="@+id/close"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignParentEnd="true"
    android:layout_alignTop="@+id/baner3img"
    android:layout_marginEnd="15dp"
    android:visibility="visible"
    app:srcCompat="@drawable/closeee" />
</RelativeLayout>

2 个答案:

答案 0 :(得分:3)

而不是:

pw = new PopupWindow(layout, ViewPager.LayoutParams.MATCH_PARENT, ViewPager.LayoutParams.MATCH_PARENT,true);

使用:LinearLayout.LayoutParams.MATCH_PARENT并将其设置为PopupWindow

 // Creating the PopupWindow
   pw = new PopupWindow(MainActivity.this);
   pw.setContentView(layout);
   pw.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
   pw.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);

修改

同时将android:scaleType="fitXY"设置为imageview以填充其空间

<ImageView
        android:id="@+id/baner3img"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:background="@android:color/transparent"
        android:padding="0dp"
        android:scaleType="fitXY"
        android:src="@mipmap/ic_launcher" />

答案 1 :(得分:0)

我发现解决方案将图像比例设置为XY