如何让Android弹出窗口与屏幕宽度匹配

3
我在我的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个回答

9

改为:

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" />

将相对布局android:layout_width="wrap_content"更改为match_parent...并添加任何背景颜色。 - rafsanahmad007
现在弹出窗口大小相同,全屏显示,并覆盖其上的背景颜色。 - Nikola Božić
这是由于您的ImageView大小问题导致的...请将以下属性设置到您的第一个ImageView上:android:background="@android:color/transparent" android:padding="0dp" android:scaleType="fitXY"... - rafsanahmad007

0

我找到了解决方案,将图像比例设置为XY


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接