Android学习笔记上下文菜单

布局文件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学习笔记上下文菜单

上一篇:Android连载12-完善新闻app内容区域


下一篇:djang项目中的疑问及解决办法(ValueError: Invalid model reference 'apps.user.User'. String model references must be of the form 'app_label.ModelName'.)