2017-04-26 82 views
0

我在我的Activity中实现了弹出窗口。问题是我想要设置弹出窗口匹配屏幕大小的宽度,现在我在两边留下了空间。这是现在的样子Popup picture sample如何android弹出窗口匹配屏幕大小宽度

这里是我的弹出代码:

    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

相反的:

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

现在弹出相同梦诗,芽我得到完全透明的窗口... –

+0

改变你的相对布局'android:layout_width =“wrap_content”'到'match_parent' ...并添加任何背景颜色 – rafsanahmad007

+0

现在弹出相同的大小,全屏与背景颜色在它 –

0

我找到了解决方案设置图像缩放到XY