布局文件main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<!--标题栏-->
<ImageView
android:id="@+id/img_title"
android:layout_width="match_parent"
android:layout_height="@dimen/title_height"
android:scaleType="fitXY"
android:src="@drawable/wei_top" />
<!--头像-->
<ImageView
android:id="@+id/img_head"
android:layout_width="@dimen/head"
android:layout_height="@dimen/head"
android:layout_below="@+id/img_title"
android:src="@mipmap/tx3"/>
<!--用户名-->
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/img_head"
android:text="CSDN梦梦"
android:textSize="@dimen/userNameSize"
android:layout_marginLeft="@dimen/margin_headLeft"
android:layout_below="@+id/img_title"
android:textColor="@color/colorUsername"/>
<!--内容-->
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/content"
android:layout_below="@+id/username"
android:layout_toRightOf="@+id/img_head"
android:textColor="@color/colorContent"
android:textSize="@dimen/content_textSize"
android:layout_marginLeft="@dimen/margin_headLeft"/> <TextView
android:id="@+id/time_tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="12小时前"
android:layout_toRightOf="@+id/img_head"
android:layout_marginLeft="@dimen/margin_headLeft"
android:layout_marginTop="10dp"
android:textColor="@android:color/darker_gray"
android:layout_below="@id/content"/>
</RelativeLayout>

菜单文件menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_copy" android:title="复制"></item>
<item android:id="@+id/menu_collect" android:title="收藏"></item>
<item android:id="@+id/menu_translate" android:title="翻译"></item>
<item android:id="@+id/menu_report" android:title="举报"></item>
</menu>

MainActivity.java

package com.lzp.circleoffriend;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends AppCompatActivity { TextView content; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//为文本框注册上下文菜单
TextView content = findViewById(R.id.content);
registerForContextMenu(content);
} //重写onCreateContextMunu()方法,添加一个上下文菜单
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.menu,menu);
}
//重写onContextItemSelected,对每个菜单的选中事件做处理
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.menu_copy:
Toast.makeText(MainActivity.this,
"已复制",Toast.LENGTH_SHORT).show();
break;
case R.id.menu_collect:
Toast.makeText(MainActivity.this,
"已收藏",Toast.LENGTH_SHORT).show();
break;
}
return super.onContextItemSelected(item);
}
}

效果:

Android学习笔记上下文菜单的更多相关文章

  1. android学习笔记23——菜单

    菜单在桌面应用程序中使用非常广泛,由于手机屏幕的制约,菜单在手机应用中减少不少. android应用中的菜单默认是不可见的,只有当用户单击手机上“Menu”键时,系统才会显示该应用关联的采用项. an ...

  2. Android学习笔记--Menu菜单的使用

    实现选项菜单.上下文菜单,以及菜单内部的子菜单. 视图效果: MainActivity 选项菜单 选项菜单的子菜单 上下文菜单(按住按钮或者EditText弹出) 注意:上下文菜单如何弹出?在注册该菜 ...

  3. 【转】 Pro Android学习笔记(三五):Menu(6):XML方式 & PopUp菜单

    目录(?)[-] 利用XML创建菜单 XML的有关属性 onClick事件 Pop-up菜单 利用XML创建菜单 在代码中对每个菜单项进行设置,繁琐且修改不灵活,不能适配多国语言的要求,可以利用资源进 ...

  4. 【转】 Pro Android学习笔记(三三):Menu(4):Alternative菜单

    目录(?)[-] 什么是Alternative menu替代菜单 小例子说明 Alternative menu代码 关于Category和规范代码写法 关于flags 多个匹配的itemId等参数 什 ...

  5. Android学习笔记——Menu(二)

    知识点: 这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu). 上下文菜单 上下文菜单提供对UI界面上的特定项或上下文框架的操作,就 ...

  6. Pro Android学习笔记 ActionBar(1):Home图标区

     Pro Android学习笔记(四八):ActionBar(1):Home图标区 2013年03月10日 ⁄ 综合 ⁄ 共 3256字 ⁄ 字号 小 中 大 ⁄ 评论关闭 ActionBar在A ...

  7. 【转】 Pro Android学习笔记(五六):配置变化

    目录(?)[-] Activity的destorycreate过程 Fragment的destorycreate过程 onSaveInstanceState saveFragmentInstanceS ...

  8. 【转】 Pro Android学习笔记(五二):ActionBar(5):list模式

    可以在action bar中加入spinner的下来菜单,有关spinner,可以参考Pro Android学习笔记(二十):用户界面和控制(8):GridView和Spinner. list的样式和 ...

  9. 【转】Pro Android学习笔记(三十):Menu(1):了解Menu

    目录(?)[-] 创建Menu MenuItem的属性itemId MenuItem的属性groupId MenuItem的属性orderId MenuItem的属性可选属性 Menu触发 onOpt ...

随机推荐

  1. step over、step into、step into my code、step out、run to cursor

    红 step over 跳过子函数 黄 step into 进入子函数 蓝 step into my code 不执行源码的子函数执行自己的 黑 step out 跳出当前函数 绿 run to cu ...

  2. 调用 start_kernel

    步骤 1 关闭中断.进入 SVC 模式 ENTRY(stext) THUMB( adr r9, BSYM(1f) ) @ Kernel is always entered in ARM. THUMB( ...

  3. 【Copy攻城狮日志】docker搭建jenkins拉取svn代码打包vue项目部署到nginx

    ↑开局一张图,故事全靠编↑ 前言 打开搜索引擎输入『Copy攻城狮』,发现最新的一条记录已经是去年的4月,意味着我又有一年时间没有再总结成长了.习惯了“温水煮青蛙”的日子,无论是经验水平还是薪资收入, ...

  4. web自动化的一些基础知识

    selenium 原理 就是通过webdriver 给浏览器的驱动发送命令,打开浏览器,建立http通信请求 然后通过发送各种命令让浏览器进而执行各种操作 xpath 语法 #xpath定位总结:'' ...

  5. Linux系统部署JavaWeb项目(超详细tomcat,nginx,mysql)

    转载自:Linux系统部署JavaWeb项目(超详细tomcat,nginx,mysql) 我的系统是阿里云的,香港的系统,本人选择的是系统镜像:CentOS 7.3 64位. 具体步骤: 配置Jav ...

  6. CopyOnWriteArrayList(写入并复制) & CountDownLatch(闭锁)

    ConcurrentHashMap: ①Java 5.0 在 java.util.concurrent 包中提供了多种并发容器类来改进同步容器 的性能.② ConcurrentHashMap 同步容器 ...

  7. Javascript函数闭包详解(通俗易懂

    许多书上闭包过于复杂讲解难懂,自己理解了一下并总结啦~ 讲闭包之前,需要先明白以下几个概念. 总之,函数执行时所在的作用域,是定义时的作用域,而不是调用时所在的作用域. 1.执行上下文(executi ...

  8. Typora 使用 Markdown 嵌入 LaTeX 数学公式符号语法

    博客园不支持渲染 LaTeX 数学公式,需要用到什么公式,请复制到您所用的支持 LaTeX 的编辑器中查看实现效果.Typora 可以渲染 LaTeX 数学公式. 目录 行内与独行 行内公式 独行公式 ...

  9. Java实现 蓝桥杯 算法提高 菱形

    试题 算法提高 菱形 请编程输出一个菱形.输入为菱形中心到顶点的距离 样例输入 2 样例输出 import java.util.Scanner; public class Main { public ...

  10. Java实现 第十一届 蓝桥杯 (本科组)省内模拟赛

    有错误的或者有问题的欢迎评论 计算机存储中有多少字节 合法括号序列 无向连通图最少包含多少条边 字母重新排列 凯撒密码加密 反倍数 正整数的摆动序列 螺旋矩阵 小明植树 户户通电 计算机存储中有多少字 ...