gpt4 book ai didi

android - 如何android弹出窗口匹配屏幕尺寸宽度

转载 作者:行者123 更新时间:2023-11-29 15:01:44 27 4
gpt4 key购买 nike

我在我的 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>

最佳答案

代替:

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 - 如何android弹出窗口匹配屏幕尺寸宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43637608/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com