我想通过点击窗外或按后退按钮来关闭PopupWindow。现在你可以点击屏幕上的任何地方,窗口关闭,我无法弄清楚原因。我还想为PopupWindow添加一个阴影。谢谢!
MainActivity.Java:
public class MainActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMarkerClickListener{
LinearLayout mainLayout;
private GoogleMap mMap;
PlaceAutocompleteFragment placeAutoComplete;
private static final String TAG = "Main Activity";
List<Marker> list = new ArrayList<>();
Marker NY_MANHATTAN;
Marker NY_BROOKLYN;
Marker NY_BRONX;
Marker NY_QUEENS;
Marker NY_STATENISLAND;
private PopupWindow popupWindow;
private LayoutInflater layoutInflater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//TODO:initialize startup stuff here:
/*
* layoutTest is the testing inflater for all popup windows.
* here is where new popup windows will be added to the list.
* */
mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
placeAutoComplete = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.place_autocomplete);
placeAutoComplete.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
Log.i(TAG, "Place Selected: " + place.getName());
LatLng latLng = place.getLatLng();
//move map camera
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 11));
}
@Override
public void onError(Status status) {
Log.d("Maps", "An error occurred: " + status);
}
});
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
try {
// Customise the styling of the base map using a JSON object defined
// in a raw resource file.
boolean success = googleMap.setMapStyle(
MapStyleOptions.loadRawResourceStyle(
this, R.raw.style_json));
if (!success) {
Log.e(TAG, "Style parsing failed.");
}
} catch (Resources.NotFoundException e) {
Log.e(TAG, "Can't find style. Error: ", e);
}
//TODO: DEFINE MARKER LOCATIONS HERE:
LatLng manhattanLoc = new LatLng(40.754932, -73.984016);
LatLng brooklynLoc = new LatLng(40.650002, -73.949997);
LatLng bronxLoc = new LatLng(40.837048, -73.865433);
LatLng queensLoc = new LatLng(40.742054, -73.769417);
LatLng statenIslandLoc = new LatLng(40.579021, -74.151535);
//TODO: INIT MARKERS HERE:
NY_MANHATTAN = googleMap.addMarker(new MarkerOptions().position(manhattanLoc).title("Manhattan").snippet("test"));
list.add(NY_MANHATTAN);
NY_BROOKLYN = googleMap.addMarker(new MarkerOptions().position(brooklynLoc).title("Brooklyn").snippet("test"));
list.add(NY_BROOKLYN);
NY_BRONX = googleMap.addMarker(new MarkerOptions().position(bronxLoc).title("Bronx").snippet("test"));
list.add(NY_BRONX);
NY_QUEENS = googleMap.addMarker(new MarkerOptions().position(queensLoc).title("Queens").snippet("test"));
list.add(NY_QUEENS);
NY_STATENISLAND = googleMap.addMarker(new MarkerOptions().position(statenIslandLoc).title("Staten Island").snippet("test"));
list.add(NY_STATENISLAND);
googleMap.setOnCameraChangeListener(new
GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition cameraPosition) {
for (Marker m : list) {
m.setVisible(cameraPosition.zoom > 10); // <-- define zoom level for markers here
/**
* Zoom level guidelines:
* 12 = zoomed in about 90%
* 1 = zommed in less than 5%
* Higher = higher zoom, Lower = lower zoom.
* Confusing. I know.
*/
}
}
});
mMap.setOnMarkerClickListener(this);
}
//TODO: handle marker clicks below:
/*
* current placeholder code states that all markers
* will open the popup_test layout.
* */
public boolean onMarkerClick(Marker arg0) {
PopupInit();
return false;
}
public void PopupInit()
{
layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
ViewGroup container = (ViewGroup) layoutInflater.inflate(R.layout.popup_test,null);
/*
*
* set x/y locations here:
*
* */
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int popWidth = dm.widthPixels;
int popHeight = dm.heightPixels;
popupWindow = new PopupWindow(container,(int) (popWidth*.8),(int) (popHeight*.6),true);
popupWindow.showAtLocation(mainLayout, Gravity.CENTER,0,0);
container.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
popupWindow.dismiss();
return true;
}
});
}
}
Popup_Test.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#fff55250"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
对noob问题抱歉。我已经有一段时间了,而且无论我尝试多少,我似乎无法弄明白...我最大的两个问题是在弹出窗口后面留下阴影并在新闻界关闭它后退按钮或弹出窗口外部。首先十分感谢。感谢。
答案 0 :(得分:1)
您需要使用和外部触摸true
popWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable())
popupWindow.setOutsideTouchable(true);
传递任何drawable
的方法。否则整个屏幕就是你的背景,外面点击将无法正常工作backPressed
将自动使用这种方式!
要为您添加阴影PopupWindow
,更简单的方法是将CardView
作为View
的{{1}}的根,并更改xml
属性。要使用elevation
,您可能还需要在gradle文件中添加依赖项
CardView
您还必须在 <android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
card_view:cardElevation="4dp"
card_view:cardCornerRadius="4dp">
<!--YOUR RELATIVE LAYOUT SHOULD BE HERE!-->
</android.support.v7.widget.CardView>
文件中添加依赖项,例如:
app gradle
CardView and elevation here中的更多详情。
这就是我的代码如何看待
dependencies {
...
compile 'com.android.support:cardview-v7:21.0.+'
}
答案 1 :(得分:0)
我可以覆盖onBackPressed()
中的Activity
方法。话虽这么说,如果按下你的后退按钮,这个方法就会被触发。在该方法中,您可以检查弹出窗口是否正在显示,并在需要时将其关闭,如下所示:
@Override
public void onBackPressed() {
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
}
super.onBackPressed();
}
您可以使用您希望用户点击取消弹出窗口的某个dismiss()
来实现onClickListeners
逻辑。
答案 2 :(得分:0)
如果您出于某种原因不想使用Xenolion吸纳管,只需手动创建覆盖整个屏幕的背景幕即可。
添加任何内容。 在背景上添加onclick监听器,以关闭背景和窗口。 在popupwindow上添加onclick侦听器为null,以防止单击顶部的弹出窗口时将其关闭。
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</LinearLayout>
<LinearLayout
android:id="@+id/popUp"
android:layout_width="500dp"
android:layout_height="500dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="@+id/backdrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
backdrop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// code to close backdrop and pop up
}
};)
//这可以防止通过单击弹出窗口来关闭窗口
popup.setOnClickListener(null);
答案 3 :(得分:0)
我在 Android 5 上使用后退按钮或点击外部都不会关闭弹出窗口。我在较新的 Android 版本上工作得很好,只是在 Android 5、Lollipop、API 级别 21 上没有。
就我而言,所需要的只是:
popupWindow.setBackgroundDrawable(new ColorDrawable())
去图!