我想在我的代码中显示一个弹出窗口,这样无论我触摸屏幕的哪个位置,弹出窗口都应该显示在我触摸的位置的正上方。不确定,如何实现这一目标。这是我当前的弹出窗口代码:
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View customView = inflater.inflate(R.layout.popup,null);
final PopupWindow popupWindow = new PopupWindow(
customView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
// Set an elevation value for popup window
// Call requires API level 21
if(Build.VERSION.SDK_INT>=21){
popupWindow.setElevation(5.0f);
}
final TextView placename = (TextView) customView.findViewById(R.id.popup_id) ;
Button closeButton = (Button) customView.findViewById(R.id.directions);
placename.setText(place.getName());
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
// Set a click listener for the popup window close button
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Dismiss the popup window
popupWindow.dismiss();
}
});
popupWindow.setFocusable(true);
popupWindow.showAtLocation(mapInsideContainer, Gravity.CENTER, 0, 0);
popupWindow.showAsDropDown(customView);
这是我的 popup.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@android:color/background_light">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="1dp"
android:background="@android:color/white">
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="20dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:id="@+id/popup_id" />
<Button
android:id="@+id/directions"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/black_overlay"
android:textColor="@android:color/white"
android:text="Directions" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
由于 Gravity.CENTER,目前它显示在屏幕中央,但我想将它显示在我在屏幕上动态触摸的位置的正上方。有任何想法吗?谢谢
如果你能指导我创建一个顶部有标题的弹出式聊天气泡,底部有一个“放置”按钮,气泡指针位于屏幕上单击的位置,则可加分
最佳答案
我就是这样做的,希望对你有帮助
customView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
popupWindow.showAtLocation(getWindow().getDecorView(), Gravity.NO_GRAVITY, (int)event.getX(), (int)event.getY() - customView.getMeasuredHeight());
并覆盖 public boolean onTouchEvent(MotionEvent event)
@Override
public boolean onTouchEvent(MotionEvent event) {
popup(event);
return super.onTouchEvent(event);
}
编辑: 我的写作如下,这对我有用。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
popup(event);
return super.onTouchEvent(event);
}
private void popup(MotionEvent event) {
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View customView = inflater.inflate(R.layout.popup,null);
final PopupWindow popupWindow = new PopupWindow(
customView,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
// Set an elevation value for popup window
// Call requires API level 21
if(Build.VERSION.SDK_INT>=21){
popupWindow.setElevation(5.0f);
}
final TextView placename = (TextView) customView.findViewById(R.id.popup_id) ;
Button closeButton = (Button) customView.findViewById(R.id.directions);
placename.setText("Name");
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
// Set a click listener for the popup window close button
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Dismiss the popup window
popupWindow.dismiss();
}
});
// popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
customView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
popupWindow.showAtLocation(getWindow().getDecorView(), Gravity.NO_GRAVITY, (int)event.getX(), (int)event.getY() - customView.getMeasuredHeight());
getWindow().getDecorView().setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/parent"
android:background="@color/colorAccent"
android:fitsSystemWindows="true">
</android.support.constraint.ConstraintLayout>
关于android - 如何在我在屏幕上触摸的地方的正上方显示一个弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44147582/