简体   繁体   中英

Set Popup menu to full screen

Note : This is popup menu not popup window.So I request you all to read carefully.

I have implemented pop menu. It is displaying in half of the screen. I want to spread this to entire width of device. I tried to change its style by setting layout_width as match_parent but with no success.

Below is what I tried so far:

Style

 <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->

        <item name="popupMenuStyle">@style/PopupMenu</item>
    </style>

  <!-- Change Overflow Menu Background -->
    <style name="PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
        <item name="android:popupBackground">#888888</item>
        <item name="android:layout_width">match_parent</item>
    </style>

Below is my java code:

  PopupMenu menu = new PopupMenu(getActivity(), tvnext);


    for (int i = 0; i < array.size(); i++) {

        menu.getMenu().add(1, i, 1, array.get(i).getAccountName());

    }


    menu.show();

    menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            setUpNextFunctionality(item.getItemId());


            return false;
        }
    });

PS : Please don't suggest me to use popup window. This is my last option if nothing work.

I got your question. Instead of modify something use new widget which can easily fulfill your necessary and compatible with newer version.

Google introduce new Material concept as Bottom-Sheet

To use it in android you can use git hub libraries like this .

I don't think you can do that without implementing of your own popup window similar to PopupMenu . If you will check MenuPopupHelper::createPopup :

@NonNull
private MenuPopup createPopup() {
    final WindowManager windowManager = (WindowManager) mContext.getSystemService(
            Context.WINDOW_SERVICE);
    final Display display = windowManager.getDefaultDisplay();
    final Point displaySize = new Point();

    if (Build.VERSION.SDK_INT >= 17) {
        display.getRealSize(displaySize);
    } else if (Build.VERSION.SDK_INT >= 13) {
        display.getSize(displaySize);
    } else {
        displaySize.set(display.getWidth(), display.getHeight());
    }

    final int smallestWidth = Math.min(displaySize.x, displaySize.y);
    final int minSmallestWidthCascading = mContext.getResources().getDimensionPixelSize(
            R.dimen.abc_cascading_menus_min_smallest_width);
    final boolean enableCascadingSubmenus = smallestWidth >= minSmallestWidthCascading;

    final MenuPopup popup;
    if (enableCascadingSubmenus) {
        popup = new CascadingMenuPopup(mContext, mAnchorView, mPopupStyleAttr,
                mPopupStyleRes, mOverflowOnly);
    } else {
        popup = new StandardMenuPopup(mContext, mMenu, mAnchorView, mPopupStyleAttr,
                mPopupStyleRes, mOverflowOnly);
    }

    // Assign immutable properties.
    popup.addMenu(mMenu);
    popup.setOnDismissListener(mInternalOnDismissListener);

    // Assign mutable properties. These may be reassigned later.
    popup.setAnchorView(mAnchorView);
    popup.setCallback(mPresenterCallback);
    popup.setForceShowIcon(mForceShowIcon);
    popup.setGravity(mDropDownGravity);

    return popup;
}

you will see that size of PopupMenu kind of hardcoded as per display size. So probably easy way is to check PopupMenu related source code and implement something similar, but with sizes which you'd like to have.

Trythis:

pwindow = 
    new PopupWindow(layoutt,LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,true);

尝试在您的样式中设置minWidth,如下所示

     <item name="android:minWidth">1000dp</item>

try this ,

inflater.inflate(R.layout.menu, menu);

or

menu.inflate(R.layout.popup_menu);

Make it translucent. I made same for pop up like this. Please check this one. May be it also work for you.

In your manifest:

    <activity
            android:name=".ActivityInviteFriend"
            android:theme="@style/Theme.TransparentInfo">

        </activity>

In style: main theme:

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/LightappActionBarColor</item>
        <item name="colorPrimaryDark">@color/appColor</item>
        <item name="colorAccent">@color/btn_color</item>
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
    </style>

and your custom theme:

    <color name="semiTransparentBlack">#00000000</color>

    <style name="Theme.TransparentInfo" parent="AppTheme">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@color/semiTransparentBlack</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:backgroundDimEnabled">true</item>
    </style>

Your Activity:

package com.gc.naifizzy;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

public class ActivityInviteFriend extends AppCompatActivity {
    Button btn_send;
    Snackbar snackbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawable(
                new ColorDrawable(android.graphics.Color.TRANSPARENT));
        setContentView(R.layout.activity_invite_friend);
        btn_send = (Button) findViewById(R.id.btn_send);
        btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //snack("Invitation sent successfully...");
                Toast.makeText(ActivityInviteFriend.this, "Invitation sent successfully", Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }

    public void snack(String data) {
        snackbar = Snackbar
                .make(findViewById(android.R.id.content), data, Snackbar.LENGTH_LONG)
                .setAction("Dismiss", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        snackbar.dismiss();
                    }
                });

        snackbar.show();

    }
}

and finally your xml layout :

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:background="#502f2f2f">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_centerInParent="true"
        android:background="@android:color/white"
        android:paddingLeft="5dp"
        android:paddingRight="5dp">

        <RelativeLayout
            android:id="@+id/dds"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:background="#112f2f2f"
            android:paddingTop="10dp"
            android:visibility="visible">


            <TextView
                android:id="@+id/txt_what"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerVertical="true"
                android:gravity="center_vertical|left"
                android:paddingLeft="10dp"
                android:text="Invite people to join your tree on Naifizzy"
                android:textColor="@color/appColor"
                android:textSize="15sp"
                android:textStyle="bold" />

        </RelativeLayout>

        <View
            android:id="@+id/view"
            android:layout_width="match_parent"
            android:layout_height="3dp"
            android:layout_below="@id/dds"
            android:layout_marginTop="3dp"
            android:alpha="0.5"
            android:background="@color/appColor"
            android:visibility="visible" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/view"
            android:layout_marginTop="20dp"
            android:gravity="center_vertical|center_horizontal"
            android:orientation="vertical">

            <TextView
                android:id="@+id/txt_share"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="10dp"
                android:text="Invite people by sharing this link "
                android:textColor="#992f2f2f"
                android:textSize="15sp" />

            <EditText
                android:id="@+id/edt_user_link"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_below="@id/txt_share"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/layout_border"
                android:editable="false"
                android:paddingLeft="10dp"
                android:singleLine="true"
                android:text="http://naifizzy.com/@parik_dhakan"
                android:textColor="#992f2f2f"
                android:textSize="15sp" />

            <View
                android:id="@+id/view1"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_below="@id/edt_user_link"
                android:layout_marginBottom="20dp"
                android:layout_marginTop="20dp"
                android:alpha="0.5"
                android:background="@color/appColor" />

            <TextView
                android:id="@+id/txt_share2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/view1"
                android:layout_centerVertical="true"
                android:layout_marginLeft="10dp"
                android:text="Send by email "
                android:textColor="#992f2f2f"
                android:textSize="15sp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginBottom="30dp"
                android:layout_marginTop="10dp"
                android:orientation="horizontal"
                android:visibility="visible"
                android:weightSum="1">

                <LinearLayout
                    android:id="@+id/ed_l"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="0.2"
                    android:background="@drawable/layout_border"
                    android:orientation="horizontal"
                    android:visibility="visible">

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">


                        <android.support.design.widget.TextInputLayout
                            android:id="@+id/edl_current"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_centerInParent="true"

                            android:paddingTop="5dp"
                            android:textColorHint="#992f2f2f"
                            android:textSize="15sp">

                            <EditText
                                android:id="@+id/edt_mail"
                                style="@style/StyledTilEditText"
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:layout_centerHorizontal="true"
                                android:background="@android:color/transparent"
                                android:hint="Enter recipients' Email seprated by commas "
                                android:inputType="textEmailAddress"
                                android:paddingLeft="10dp"
                                android:textColor="#992f2f2f"
                                android:textColorHint="#992f2f2f"

                                android:textSize="15sp" />
                        </android.support.design.widget.TextInputLayout>

                        <ImageView
                            android:id="@+id/img_show"
                            android:layout_width="30dp"
                            android:layout_height="25dp"
                            android:layout_alignParentEnd="true"
                            android:layout_centerVertical="true"
                            android:layout_marginEnd="20dp"
                            android:scaleType="centerInside"
                            android:src="@drawable/ic_eye"
                            android:visibility="gone" />
                    </RelativeLayout>
                </LinearLayout>

                <EditText
                    android:id="@+id/edt_user_link2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@id/txt_share2"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:background="@drawable/layout_border"

                    android:paddingLeft="10dp"
                    android:singleLine="true"
                    android:text="http://naifizzy.com/@parik_dhakan"
                    android:textColor="#992f2f2f"
                    android:textSize="15sp"
                    android:visibility="gone" />

                <Button
                    android:id="@+id/btn_send"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginRight="5dp"
                    android:layout_weight="0.8"
                    android:background="@drawable/signupbg"
                    android:gravity="center_vertical|center_horizontal"
                    android:text="Send\nInvites"
                    android:textColor="@android:color/white"
                    android:textSize="10sp" />
            </LinearLayout>


        </LinearLayout>
    </RelativeLayout>


</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM