安卓 专高 day02 菜单和弹窗

菜单

系统菜单(OptionsMenu)

在res下面创建一个menu文件夹,并新建一个xml文件作为OptionMenu的布局文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/big" android:title="大力" app:showAsAction="always"> </item>
    <item android:id="@+id/dog" android:title="成果" app:showAsAction="ifRoom"> </item>
</menu>

Activity重写onCreateOptionsMenu加载资源文件

//系统菜单 OptionsMenu
    //Activity重写onCreateOptionsMenu加载资源文件
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    //Activity重写onOptionsItemSelected设置事件监听
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId){
            case R.id.big:
                Toast.makeText(this, ""+item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
                
            case R.id.dog:
                Toast.makeText(this, ""+item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

上下文菜单(ContextMenu)

和上面步骤一样但是增加了一个添加长按属性并将菜单绑定到控件上

 private TextView tvShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvShow = (TextView) findViewById(R.id.tv_show);
        //给控件添加长按
        registerForContextMenu(tvShow);
    }
//上下文菜单ContextMenu
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        getMenuInflater().inflate(R.menu.menu,menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId){
            case R.id.big:
                Toast.makeText(this, ""+item.getTitle(), Toast.LENGTH_SHORT).show();
                break;

            case R.id.dog:
                Toast.makeText(this, ""+item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onContextItemSelected(item);
    }

弹出菜单

private TextView tvXian;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        tvXian = (TextView) findViewById(R.id.tv_xian);
        //点击事件
        tvXian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupMenu();
            }
        });
    }

    public void showPopupMenu(){
        //创建对象
        //参数一 上下文  参数二  菜单显示在指定控件的下方
        PopupMenu popupMenu = new PopupMenu(this,tvXian);
        //记载布局
        popupMenu.inflate(R.menu.menu);
        //监听事件
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                int itemId = item.getItemId();
                switch (itemId){
                    case R.id.big:
                        //点击是文本框文字变颜色
                        tvXian.setTextColor(Color.parseColor("#000999"));
                        break;
                    case R.id.dog:
                        tvXian.setTextColor(Color.parseColor("#999111"));
                        break;
                }
                return false;
            }
        });
        //显示
        popupMenu.show();
    }

弹出窗口

布局文件

<?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=".Main3Activity">

    <TextView
        android:id="@+id/tv_xin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="弹出窗口"
        android:textSize="30sp"
        />
</LinearLayout>

java代码

public class Main3Activity extends AppCompatActivity {
    private TextView tvXin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main3);
        tvXin = (TextView) findViewById(R.id.tv_xin);
        tvXin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建对象
                PopupWindow popupWindow = new PopupWindow(Main3Activity.this);
                //加载布局
                View view = LayoutInflater.from(Main3Activity.this).inflate(R.layout.tan_layout, null);
                //设置三要素,缺一不可.
                popupWindow.setContentView(view);
                popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.setWidth(600);
                //点击外部取消
                popupWindow.setOutsideTouchable(true);
                //设置弹出位置
                popupWindow.showAsDropDown(tvXin,200,200);//x轴y轴偏移距离
            }
        });
    }
}

弹出的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我的大力"
        android:textSize="20sp"
        />
    <ImageView
        android:layout_width="80dp"
        android:layout_height="100dp"
        android:src="@drawable/a8"
        />
</LinearLayout>

发布了2 篇原创文章 · 获赞 1 · 访问量 486

猜你喜欢

转载自blog.csdn.net/zxsenqiu/article/details/104432438