国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

Android中怎么實(shí)現長(cháng)按照片彈出右鍵菜單功能

發(fā)布時(shí)間:2021-09-27 17:50 來(lái)源:億速云 閱讀:0 作者:小新 欄目: 開(kāi)發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)Android中怎么實(shí)現長(cháng)按照片彈出右鍵菜單功能的內容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

實(shí)現

將布局改為L(cháng)inearLayout,并通過(guò)android:orientation="vertical">設置為垂直布局。

然后添加一個(gè)ImageView,并設置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>

然后來(lái)到Activity,首先在activity中重寫(xiě)onCreateContextMenu方法,此方法能新增菜單,并添加菜單項

//在activity中重寫(xiě)onCreateContextMenu菜單,為菜單添加選項值  @Override  public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {    super.onCreateContextMenu(menu, v, menuInfo);    menu.add("收藏");    menu.add("舉報");  }

然后在onCreate方法中將長(cháng)按事件注冊到菜單中并打開(kāi)菜單。

@Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_long_click);    //將長(cháng)按事件注冊到菜單中,并打開(kāi)菜單    ImageView imageView = (ImageView) findViewById(R.id.image);    imageView.setOnLongClickListener(new View.OnLongClickListener() {      @Override      public boolean onLongClick(View v) {        //注冊菜單        registerForContextMenu(v);        //打開(kāi)菜單        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);    //將長(cháng)按事件注冊到菜單中,并打開(kāi)菜單    ImageView imageView = (ImageView) findViewById(R.id.image);    imageView.setOnLongClickListener(new View.OnLongClickListener() {      @Override      public boolean onLongClick(View v) {        //注冊菜單        registerForContextMenu(v);        //打開(kāi)菜單        openContextMenu(v);        return true;      }    });  }  //在activity中重寫(xiě)onCreateContextMenu菜單,為菜單添加選項值  @Override  public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {    super.onCreateContextMenu(menu, v, menuInfo);    menu.add("收藏");    menu.add("舉報");  }}

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。

国产午夜无码精品免费看| 久久丫精品久久丫| 亚洲国产精品无码久久网速快| GAY男男帅哥网站射精| 伊人精品久久久久7777| 夜夜精品无码一区二区三区|