Android学习笔记上下文菜单

Android学习笔记上下文菜单,第1张

概述布局文件main_activity.xml<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apkes/android"xmlns:app="http://schemas.android.com/apkes-auto"xmlns:tool

布局文件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学习笔记上下文菜单全部内容,希望文章能够帮你解决Android学习笔记上下文菜单所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1060862.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存