Android カスタムドロップダウン/ポップアップメニュー 質問する

Android カスタムドロップダウン/ポップアップメニュー 質問する

ボタンに固定されたカスタム ドロップダウン/ポップアップ メニューを作成するにはどうすればよいですか?

ポップアップ メニュー (ビューに固定) のように動作し、メニューから項目をクリックしたときに何かを実行する必要があります。

コードでメニューに項目を追加し、メニューの高さを維持し、項目が 5 つ以上ある場合にスクロール可能にするにはどうすればよいでしょうか。画像を追加する必要はなく、テキストのみを追加する必要があります。

ここに画像の説明を入力してください

ベストアンサー1

アップデート: KotlinでAndroidにポップアップメニューを作成するには、私の回答を参照してくださいここ

Java を使用して Android でポップアップ メニューを作成するには:

ボタンが 1 つだけ含まれるディレクトリactivity_main.xmlの下にレイアウト ファイルを作成します。res/layout

ファイル名:activity_main.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"  
    android:paddingBottom="@dimen/activity_vertical_margin"  
    android:paddingLeft="@dimen/activity_horizontal_margin"  
    android:paddingRight="@dimen/activity_horizontal_margin"  
    android:paddingTop="@dimen/activity_vertical_margin"  
    tools:context=".MainActivity" >  

    <Button  
        android:id="@+id/button1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentLeft="true"  
        android:layout_alignParentTop="true"  
        android:layout_marginLeft="62dp"  
        android:layout_marginTop="50dp"  
        android:text="Show Popup" />  

</RelativeLayout>  

ディレクトリpopup_menu.xmlの下にファイルを作成するres/menu

以下に示す 3 つの項目が含まれています。

ファイル名:poupup_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >  

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

    <item  
        android:id="@+id/two"  
        android:title="Two"/>  

    <item  
        android:id="@+id/three"  
        android:title="Three"/>  

</menu>  

ボタンをクリックするとポップアップ メニューを表示する MainActivity クラス。

ファイル名:MainActivity.java

public class MainActivity extends Activity {  
    private Button button1;  

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

        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //Creating the instance of PopupMenu
                PopupMenu popup = new PopupMenu(MainActivity.this, button1);
                //Inflating the Popup using xml file
                popup.getMenuInflater()
                    .inflate(R.menu.popup_menu, popup.getMenu());

                //registering popup with OnMenuItemClickListener
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {
                        Toast.makeText(
                            MainActivity.this,
                            "You Clicked : " + item.getTitle(),
                            Toast.LENGTH_SHORT
                        ).show();
                        return true;
                    }
                });

                popup.show(); //showing popup menu
            }
        }); //closing the setOnClickListener method
    }
}

プログラムで追加するには:

PopupMenu menu = new PopupMenu(this, view);

menu.getMenu().add("One");
menu.getMenu().add("Two");
menu.getMenu().add("Three");

menu.show();

フォローするこれプログラムでメニューを作成するためのリンク。

おすすめ記事