我想在 Popmenu 中顯示自定義布局。但我得到這個錯誤
Java.Lang.RuntimeException: '期待選單,得到 LinearLayout'
有沒有辦法將此布局膨脹到PopupMenu
? 這是我的代碼和 xml
private void ShowMenu(ImageButton anchor, Dictionary<string, object> data)
{
Android.Widget.PopupMenu menu = new Android.Widget.PopupMenu(_context, anchor);
menu.Inflate(Resource.Layout.menu_outlet_buttons);
menu.Show();
}
menu_outlets_buttons.xml
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="left"
android:background="@drawable/bg_round_corner_full">
<ImageButton
android:id="@ id/view_outlet_more_button"
android:layout_width="45dp"
android:layout_height="45dp"
android:foreground="?android:attr/selectableItemBackgroundBorderless"
android:background="@color/transparent"
android:tint="@color/primaryColor"
android:scaleType="fitCenter"
android:src="@drawable/outline_more_vert_24"
android:padding="10dp"/>
<ImageButton
android:id="@ id/view_outlet_edit_button"
android:layout_width="45dp"
android:layout_height="45dp"
android:foreground="?android:attr/selectableItemBackgroundBorderless"
android:background="@color/transparent"
android:tint="@color/primaryColor"
android:scaleType="fitCenter"
android:src="@drawable/outline_edit_24"
android:padding="10dp"/>
<Button
android:id="@ id/view_outlet_call_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Make a call"
android:background="?android:attr/selectableItemBackground"
android:backgroundTint="@color/whiteFull"
android:textColor="@color/secondaryColor"
android:textSize="15dp"
android:layout_marginRight="10dp"
android:drawableLeft="@drawable/shopping_cart_24px"
android:drawableTint="@color/secondaryColor"/>
</LinearLayout>
</LinearLayout>
這是它的樣子:
我想在 FB messenger 中做出反應
我已經在互聯網上搜索,但大部分帖子不是 Xamarin.Android。提前致謝
uj5u.com熱心網友回復:
使用彈出視窗。如何在 Xamarin android 中使用 PopupWindow?
private void ShowMenu(ImageButton anchor, Dictionary<string, object> data)
{
LayoutInflater inflater = (LayoutInflater)_context.GetSystemService(Context.LayoutInflaterService);
View view = inflater.Inflate(Resource.Layout.menu_outlet_buttons, null);
int[] location = new int[2];
anchor.GetLocationOnScreen(location);
int x = location[0];
int y = location[1];
PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent, true);
popupWindow.ShowAtLocation(view, GravityFlags.NoGravity, x, y);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/493335.html