Android自定义弹出菜单带开关

前端之家收集整理的这篇文章主要介绍了Android自定义弹出菜单带开关前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在弹出菜单自定义menuItem?我需要第一个menuitem的开关.这是我到目前为止所得到的:

menu.xml文件

<menu 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"
    tools:context="android.oli.com.fitnessapp.activities.LiveSelectActivity">

    <item
        android:icon="@drawable/ic_access_alarm_white_24dp"
        android:orderInCategory="100"
        app:showAsAction="always"
        android:visible="true"
        android:title="@string/action_settings"
        android:onClick="showPopup"/>

</menu>

menu_popup.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/one"
        android:title="One"/>

    <item
        android:id="@+id/setTime"
        android:title="Two"
        android:onClick="showTimePickerDialog"/>


</menu>

活动代码

public void showPopup(MenuItem menuItem){
        View view = findViewById(R.id.action_alarm);
        PopupMenu popup = new PopupMenu(this,view);
        MenuInflater inflater = popup.getMenuInflater();
        inflater.inflate(R.menu.menu_popup,popup.getMenu());
        popup.show();
    }

解决方法@H_404_16@
您可以使用popupwindow,因为它允许使用自定义布局.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" 
    android:padding="5dp">

<Switch
    android:id="@+id/mySwitch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="20dp"
    android:text="Play with the Switch" />

<TextView
    android:id="@+id/switchStatus"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/mySwitch"
    android:layout_marginTop="22dp"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

并在ur活动中实现此方法

/* you should refer to a view to stick your popup wherever u want.
** e.g. Button button  = (Button) findviewbyId(R.id.btn);
**     if(popupWindow != null)
**         showPopup(button); 
**/
    public void showPopup(View v) {


            LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View popupView = layoutInflater.inflate(R.layout.popup_filter_layout,null);

            popupWindow = new PopupWindow(
                    popupView,ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);

            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            popupWindow.setOutsideTouchable(true);
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    //TODO do sth here on dismiss
                }
            });

            popupWindow.showAsDropDown(v); 
}

原文链接:https://www.f2er.com/android/317040.html

猜你在找的Android相关文章