Android MenuItem自定义布局

Android MenuItem自定义布局,第1张

概述当我点击 *** 作栏中的 *** 作按钮时,我会显示一个 PopupMenu. 我想在我的PopupMenu中的MenuItem,使用这样的自定义布局布局/ menu_item_layout.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_c 当我点击 *** 作栏中的 *** 作按钮时,我会显示一个 PopupMenu.
我想在我的PopupMenu中的MenuItem,使用这样的自定义布局:

布局/ menu_item_layout.xml

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:ID="@+ID/menuItemLayout"    androID:orIEntation="horizontal" >    <ImageVIEw        androID:ID="@+ID/imageVIEwMenuItem"        androID:layout_wIDth="20dip"        androID:layout_height="20dip"        androID:src="@drawable/abc_List_focused_holo" />    <TextVIEw        androID:ID="@+ID/textVIEwMenuItem"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="TextVIEwMenuItem" /></linearLayout>

这是PopUpMenu的xml:

菜单/ pop_menu.xml

<menu xmlns:androID="http://schemas.androID.com/apk/res/androID"       xmlns:tools="http://schemas.androID.com/tools"       tools:context="apparound.actiobarpopupstylefacebook.Main" >    <item        androID:ID="@+ID/popupItem"        androID:showAsAction="ifRoom"/></menu>

在我的活动代码如下:

public voID showPopup(int IDR){VIEw menuItemVIEw = findVIEwByID(IDR);PopupMenu popup = new PopupMenu(this,menuItemVIEw);MenuInflater inflate = popup.getMenuInflater();inflate.inflate(R.menu.pop_menu,popup.getMenu());MenuItem menuItem= popup.getMenu().findItem(R.ID.popupItem);menuItem.setActionVIEw(R.layout.menu_item_layout);popup.show();}

但是当d出菜单出现时,项目为空.
我使用setActionvIEw()方法是错误的?
谢谢.

解决方法 对于自定义布局,您不能使用菜单,一个备用选项是 PopupWindow
PopupWindow popupwindow_obj = popupdisplay();popupwindow_obj.showAsDropDown(clickbtn,-40,18); // where u want show on vIEw click event popupwindow.showAsDropDown(vIEw,x,y);public PopupWindow popupdisplay() {     final PopupWindow popupWindow = new PopupWindow(this);    // inflate your layout or dynamically add vIEw    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);     VIEw vIEw = inflater.inflate(R.layout.mylayout,null);    button item = (button) vIEw.findVIEwByID(R.ID.button1);    popupWindow.setFocusable(true);    popupWindow.setWIDth(WindowManager.LayoutParams.WRAP_CONTENT);    popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);    popupWindow.setContentVIEw(vIEw);    return popupWindow;}

在名为my layout.xml的res / layout文件夹中创建此XML文件

<linearLayout    androID:layout_wIDth="fill_parent"    androID:layout_height="wrap_content"    androID:orIEntation="horizontal" >    <button        androID:ID="@+ID/button1"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="Window test" /></linearLayout>
总结

以上是内存溢出为你收集整理的Android MenuItem自定义布局全部内容,希望文章能够帮你解决Android MenuItem自定义布局所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1134239.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存