Android中實(shí)現(xiàn)長按照片彈出右鍵菜單功能的實(shí)例代碼
場(chǎng)景效果

注:
實(shí)現(xiàn)
將布局改為LinearLayout,并通過android:orientation="vertical">設(shè)置為垂直布局。
然后添加一個(gè)ImageView,并設(shè)置id屬性和圖片源。
<?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"
android:orientation="vertical"
tools:context=".LongClickActivity">
<ImageView
android:id="@+id/image"
android:src="@drawable/dog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
然后來到Activity,首先在activity中重寫onCreateContextMenu方法,此方法能新增菜單,并添加菜單項(xiàng)
//在activity中重寫onCreateContextMenu菜單,為菜單添加選項(xiàng)值
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add("收藏");
menu.add("舉報(bào)");
}
然后在onCreate方法中將長按事件注冊(cè)到菜單中并打開菜單。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_long_click);
//將長按事件注冊(cè)到菜單中,并打開菜單
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//注冊(cè)菜單
registerForContextMenu(v);
//打開菜單
openContextMenu(v);
return true;
}
});
}
完整示例代碼
package com.badao.relativelayouttest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.View;
import android.widget.ImageView;
public class LongClickActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_long_click);
//將長按事件注冊(cè)到菜單中,并打開菜單
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//注冊(cè)菜單
registerForContextMenu(v);
//打開菜單
openContextMenu(v);
return true;
}
});
}
//在activity中重寫onCreateContextMenu菜單,為菜單添加選項(xiàng)值
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add("收藏");
menu.add("舉報(bào)");
}
}
總結(jié)
以上所述是小編給大家介紹的Android中實(shí)現(xiàn)長按照片彈出右鍵菜單功能的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
Android設(shè)計(jì)模式之單例模式實(shí)例
這篇文章主要介紹了Android設(shè)計(jì)模式之單例模式實(shí)例,單例模式是運(yùn)用最廣泛的設(shè)計(jì)模式之一,在應(yīng)用這個(gè)模式時(shí),單例模式的類必須保證只有一個(gè)實(shí)例存在2023-04-04
Android中使用TextView實(shí)現(xiàn)圖文混排的方法
向TextView或EditText中添加圖像比直接添加文本復(fù)雜一點(diǎn)點(diǎn),需要用到<img>標(biāo)簽。接下來通過本文給大家介紹Android中使用TextView實(shí)現(xiàn)圖文混排的方法,希望對(duì)大家有所幫助2016-02-02
Android使用PullToRefresh完成ListView下拉刷新和左滑刪除功能
ListView下刷新刷功能相信從事Android開發(fā)的猿友們并不陌生,本文就帶領(lǐng)一些剛?cè)腴Tandroid的朋友或者一起愛分享的朋友來簡(jiǎn)單的實(shí)現(xiàn)ListView的下拉刷新和左滑刪除效果。感興趣的朋友一起看看吧2016-11-11
Android監(jiān)聽來電和去電的實(shí)現(xiàn)方法
這篇文章主要介紹了Android監(jiān)聽來電和去電的實(shí)現(xiàn)方法,涉及Android中BroadcastReceiver組件的使用及AndroidManifest.xml權(quán)限操作的相關(guān)技巧,需要的朋友可以參考下2016-08-08
Android開發(fā)之拖動(dòng)條/滑動(dòng)條控件、星級(jí)評(píng)分控件功能的實(shí)例代碼
這篇文章主要介紹了Android開發(fā)之拖動(dòng)條/滑動(dòng)條控件、星級(jí)評(píng)分控件功能的實(shí)例代碼,需要的朋友可以參考下2019-05-05
Android Activity回收與操作超時(shí)處理
這篇文章主要介紹了Android Activity回收與操作超時(shí)的相關(guān)處理操作,感興趣的小伙伴們可以參考一下2016-04-04
Android使用ItemTouchHelper實(shí)現(xiàn)側(cè)滑刪除和拖拽
這篇文章主要為大家詳細(xì)介紹了Android使用ItemTouchHelper實(shí)現(xiàn)側(cè)滑刪除和拖拽,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08

