Zuerst müssen Sie ein Layout für die Popup-windown definieren. Beispiel:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Button"
android:id="@+id/button">
</Button>
</LinearLayout>
Als nächstes Sie eine Klasse erstellen, die Ihr Popup windown
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.PopupWindow;
public class PopupMenu extends PopupWindow
{
Context m_context;
public PopupMenu(Context context)
{
super(context);
m_context = context;
setContentView(LayoutInflater.from(context).
inflate(R.layout.popup_menu, null));
setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
}
public void show(View anchor)
{
// you can edit display location is here
showAtLocation(anchor, Gravity.CENTER, 0, 0);
}
}
behandeln können Sie es einfach mit diesem Code:
PopupMenu popupMenu = new PopupMenu(context);
popupMenu.show(view);
Wenn Sie eine Listenansicht setzen in Ihr PopupWindow, das Festlegen der Breite zu WRAP_CONTENT wird nicht funktionieren. Um die eingestellte PopupWindow richtig Breite, werden Sie dies in der Show hinzufügen() -Methode:
// force the popupwindow width to be the listview width
listview.measure(
View.MeasureSpec.UNSPECIFIED,
View.MeasureSpec.UNSPECIFIED)
setWidth(listview.getMeasuredWidth());
Ich hoffe, dass es Ihr Problem helfen können! Schön, Ihnen zu helfen!
Wie dies verwendet werden kann, um das Popup-Fenster entsprechend der verfügbaren Bildschirmbreite zu platzieren. –