我可以用什么来代替 android 中的上下文菜单? [英] What can I use instead of Context Menu in android?

查看:24
本文介绍了我可以用什么来代替 android 中的上下文菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单击按钮时显示一些选项.我现在使用上下文菜单.但是显示在 Android 3 下方的上下文菜单与当前的应用程序设计不符.我想更改上下文菜单的样式或使用其他一些控件或用其他内容替换上下文菜单.我该怎么做才能使我的应用程序设计更漂亮?有没有带材料上下文菜单的材料设计库?

I want to show some options when clicking a button. I use context menu now. But the context menu showing below Android 3 is not good with the current app design. I want to either change the style of the context menu or use some other controls or replace the context menu with something else. What can I do to make my app design more beautiful? Is there any material design library with material context menu?

推荐答案

您可以使用从 API level 1 开始的 PopupWindow.

You can use the PopupWindow which starts from API level 1.

PopupWindow 中,您实际上可以使用 XML 布局文件设计一些东西.您可以包含任何内容(TextView、Button、ImageView...),例如:弹出窗口 XML :

In PopupWindow you can actually design something with XML Layout file. You can include anything (TextView,Button,ImageView,...) Like : PopUp Window 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:padding="15dp"
    android:background="#89000000"
    android:orientation="vertical">


    <Button
        android:id="@+id/idClose"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Close"
        android:layout_below="@+id/textView4"
        android:layout_alignRight="@+id/textView4"
        android:layout_alignEnd="@+id/textView4" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="PopUp Window Title"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textStyle="bold" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Item One / Option One"
        android:id="@+id/textView2"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:gravity="center" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Item Two / Option Two"
        android:id="@+id/textView3"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dp"
        android:gravity="center" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="..."
        android:id="@+id/textView4"
        android:layout_below="@+id/textView3"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:layout_marginTop="5dp" />

</RelativeLayout>

主要活动 XML:

<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">

    <Button
        android:id="@+id/idOpen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Open"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

你可以在活动中弹出它:

And you can Pop It Up in Activity like :

package com.example.popup;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;


public class MainActivity extends Activity {

    Button open;
    LayoutInflater inflater;
    View popUpView;
    PopupWindow popupWindow;
    Button close;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        open = (Button)findViewById(R.id.idOpen);
        open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.showAsDropDown(open);
            }
        });

        inflater =(LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
        popUpView = inflater.inflate(R.layout.popup_window,null);
        popupWindow = new PopupWindow(popUpView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        close = (Button)popUpView.findViewById(R.id.idClose);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });


    }
}

希望这会有所帮助,编码愉快:)

HOPE THIS WILL HELP, HAPPY CODING :)

这篇关于我可以用什么来代替 android 中的上下文菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆