欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android Studio菜单

程序员文章站 2022-03-12 07:57:51
系统菜单先在res文件项目创造mean 然后在 mean中创造.xml//mean文件
系统菜单
先在res文件项目创造mean 然后在 mean中创造.xml
//mean文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:id="@+id/it1" android:title="第一个"></item>
    <item android:id="@+id/it2" android:title="第二个"></item>
    <item android:id="@+id/it3" android:title="第三个"></item>
</menu>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity">

  <TextView
      android:id="@+id/tv"
      android:text="文字1"
      android:textSize="20sp"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"></TextView>
  <View
      android:layout_width="100dp"
      android:layout_height="match_parent"></View>
  <TextView

      android:id="@+id/tv1"
      android:text="文字2"
      android:textSize="20sp"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"></TextView>
</LinearLayout>
package com.example.day2;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private TextView tv,tv1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //上下文菜单的控件。
        tv=findViewById(R.id.tv);
        registerForContextMenu(tv);//调用上下文菜单的方法。

        //弹出菜单
        tv1=findViewById(R.id.tv1);
       tv1.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               showtext();
           }
       });
    }
    private void showtext() {
        //构造者
        final PopupMenu popupMenu=new PopupMenu(this,tv1);
        popupMenu.inflate(R.menu.menu);

        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                int itemId = menuItem.getItemId();
                switch (itemId){
                    case  R.id.it1:
                        Toast.makeText(MainActivity.this, "1", Toast.LENGTH_SHORT).show();
                        break;
                    case  R.id.it2:
                        Toast.makeText(MainActivity.this, "2", Toast.LENGTH_SHORT).show();
                        break;
                    case  R.id.it3:
                        Toast.makeText(MainActivity.this, "3", Toast.LENGTH_SHORT).show();
                        break;
                }

                return false;

            }

        });
        popupMenu.show();
    }
    //上下文菜单
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        getMenuInflater().inflate(R.menu.menu,menu);
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId){
            case R.id.it1:
                Toast.makeText(this, "1", Toast.LENGTH_SHORT).show();
                break;
            case R.id.it2:
                Toast.makeText(this, "2", Toast.LENGTH_SHORT).show();
                break;
            case R.id.it3:
                Toast.makeText(this, "3", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onContextItemSelected(item);
    }


   //系统菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId){
            case R.id.it1:
                Toast.makeText(this, "1", Toast.LENGTH_SHORT).show();
                break;
            case R.id.it2:
                Toast.makeText(this, "2", Toast.LENGTH_SHORT).show();
                break;
            case R.id.it3:
                Toast.makeText(this, "3", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    }


PopupWindow菜单
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".Main2Activity">
  <Button
      android:onClick="btt"
      android:text="开始"
      android:id="@+id/bt"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"></Button>

</LinearLayout>
package com.example.day2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class Main2Activity extends AppCompatActivity {
private  Button bt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
         bt=findViewById(R.id.bt);


    }


    public void btt(View view) {
        final PopupWindow popupWindow = new PopupWindow(this);

        View view1=LayoutInflater.from(Main2Activity.this).inflate(R.layout.item,null);
        popupWindow.setContentView(view1);

        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setWidth(600);

        TextView textView = view1.findViewById(R.id.tv1);
        TextView textView2 = view1.findViewById(R.id.tv2);
        TextView textView3 = view1.findViewById(R.id.tv3);

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(Main2Activity.this, "1", Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });
        textView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(Main2Activity.this, "2", Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });
        textView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(Main2Activity.this, "3", Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAsDropDown(bt,0,0);

    }
}

本文地址:https://blog.csdn.net/mynameisluowei/article/details/108576004