简体   繁体   English

Android弹出菜单填充父级

[英]Android Popup Menu fill parent

I try set my popup menu in way to fill hole item on grid. 我尝试设置弹出菜单以填充网格上的孔项目。 Currently it look like on attached first picture and the next one is effect which I would like to have. 目前,它看起来像在所附的第一张图片上,而下一张是我想要的效果。

在此处输入图片说明

在此处输入图片说明

My code: 我的代码:

private void showPopupMenu(View view) {
    // inflate menu
    ContextThemeWrapper ctw = new ContextThemeWrapper(context, R.style.PopupMenu);

    PopupMenu popup = new PopupMenu(ctw, view);

    Menu menu = popup.getMenu();
    menu.add(Menu.NONE, 1, Menu.NONE, "Remove");
    menu.add(Menu.NONE, 2, Menu.NONE, "Block");

    popup.setOnMenuItemClickListener(new MyMenuItemClickListener());
    popup.show();
}

Could you please point me to right direction to achieve effect from project? 您能指出我正确的方向以从项目中取得效果吗?

Demo App for your requirment by using PopupWindow. 通过使用PopupWindow满足您的要求的演示应用程序。 Preview 预习

You can add list in it or customize it according to your needs. 您可以在其中添加列表,也可以根据需要自定义列表。 MainActivity 主要活动

public class MainActivity extends Activity {

    boolean isClicked = true;
    PopupWindow popUpWindow;
    RelativeLayout relative;
    ImageView btnClickHere;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        relative = (RelativeLayout) findViewById(R.id.relative);
        popUpWindow = new PopupWindow(this);
        popUpWindow.setContentView(getLayoutInflater().inflate(R.layout.popup_design, null));
        popUpWindow.getContentView().findViewById(R.id.textViewa).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "PopItemClicked", Toast.LENGTH_LONG).show();
            }
        });

        btnClickHere = (ImageView) findViewById(R.id.imageView);
        btnClickHere.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                if (isClicked) {
                    isClicked = false;
                    popUpWindow.setHeight(relative.getHeight());
                    popUpWindow.setWidth(relative.getWidth());
                    popUpWindow.showAsDropDown(relative, 0, -relative.getHeight());
                } else {
                    isClicked = true;
                    popUpWindow.dismiss();
                }
            }
        });
    }
}

activity_main.xml activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.sohailzahid.testapp.MainActivity">

    <RelativeLayout
        android:id="@+id/relative"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@color/colorAccent"
        tools:layout_editor_absoluteX="150dp"
        tools:layout_editor_absoluteY="150dp">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:src="@android:drawable/arrow_down_float" />
    </RelativeLayout>

</RelativeLayout>

popup_design.xml popup_design.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F93567">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textViewa"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="Block"
            android:textColor="#ffffff"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/textVsiewa"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="Add to friends"
            android:textColor="#ffffff"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/textViesw"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="Remove"
            android:textColor="#ffffff"
            android:textSize="20dp" />
    </LinearLayout>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_margin="10dp"
        android:src="@android:drawable/arrow_down_float" />

</RelativeLayout>

Try this 尝试这个

   popup.getWindow().getAttributes().height = ViewGroup.LayoutParams.MATCH_PARENT;
   popup.getWindow().getAttributes().width = ViewGroup.LayoutParams.MATCH_PARENT;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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