我可以使用右键菜单什么而不是机器人?可以使用、机器人、右键菜单、而不是

2023-09-06 14:14:59 作者:难过的时候就笑吧

我想点击一个按钮时,显示某些选项。我用右键菜单了。但显示低于Android 3.0的上下文菜单中是不是好与当前应用程序的设计。我想要么改变上下文菜单的样式或使用一些其他控件或用别的东西代替的上下文菜单。我能做些什么来让我的应用程序的设计更漂亮?是否有任何材料设计库,材料的上下文菜单?

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?

推荐答案

您可以使用 PopupWindow API级别1 。

PopupWindow 实际上你可以设计一些与XML布局文件。 您可以包括任何东西(TextView的,巴顿,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:

Main Activity 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 :)

 
精彩推荐
图片推荐