溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android怎么實(shí)現(xiàn)仿微信和qq點(diǎn)擊右上角加號(hào)彈出操作框

發(fā)布時(shí)間:2022-04-13 16:03:13 來源:億速云 閱讀:352 作者:iii 欄目:編程語言

本文小編為大家詳細(xì)介紹“Android怎么實(shí)現(xiàn)仿微信和qq點(diǎn)擊右上角加號(hào)彈出操作框”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Android怎么實(shí)現(xiàn)仿微信和qq點(diǎn)擊右上角加號(hào)彈出操作框”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識(shí)吧。

Android仿微信、qq點(diǎn)擊右上角加號(hào)彈出操作框,先上圖,類似于下圖這種,點(diǎn)擊加號(hào),會(huì)彈出一個(gè)對(duì)話框,如下圖:

微信:

Android怎么實(shí)現(xiàn)仿微信和qq點(diǎn)擊右上角加號(hào)彈出操作框

自己實(shí)現(xiàn):

Android怎么實(shí)現(xiàn)仿微信和qq點(diǎn)擊右上角加號(hào)彈出操作框

接下來,我們來實(shí)現(xiàn)此功能:

其實(shí),實(shí)現(xiàn)原理就是,點(diǎn)擊“+”號(hào),彈出一個(gè)PopupWindow。

1、寫一個(gè)用于展示在ToolBar中的 menu文件,如下:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto">
  <item
    android:id="@+id/btn_msg"
    android:icon="@drawable/ic_notifications_none"
    android:title="消息"
    app:showAsAction="ifRoom" />
</menu>

2、先添加 “+” ,我的項(xiàng)目里使用的是ToolBar,我給ToolBar添加菜單,在Activity中重寫方法onCreateOptionsMenu,如下圖:

@Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_index_msg, menu);
    return super.onCreateOptionsMenu(menu);

  }

到這里,“+”號(hào),已經(jīng)出現(xiàn)了。

3、給menu添加點(diǎn)擊事件,并初始化PopupWindow,彈出自定義的PopupWindow,如下:

@Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.btn_msg:
        View popupView = IndexActivity.this.getLayoutInflater().inflate(R.layout.popupwindow, null);
        final PopupWindow window = new PopupWindow(popupView, 300, 220);
        ListView lv_msg = (ListView) popupView.findViewById(R.id.lv_msg);
        MsgAdapter msgAdapter = new MsgAdapter(context, msgBeans);
        lv_msg.setAdapter(msgAdapter);
        lv_msg.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (window.isShowing()) {
              window.dismiss();
            }
            switch (position) {
              case 0:
                if (myApplication.isOnline()) {
                  NoticeMainActivity.startActivity(IndexActivity.this);
                } else {
                  Toast.makeText(IndexActivity.this, "離線狀態(tài)不能使用此功能", Toast.LENGTH_SHORT).show();
                }
                break;
              case 1:
                if (myApplication.isOnline()) {
                  TaskMainActivity.startActivity(IndexActivity.this);
                } else {
                  Toast.makeText(IndexActivity.this, "離線狀態(tài)不能使用此功能", Toast.LENGTH_SHORT).show();
                }
                break;
              default:
                break;
            }
          }
        });
        window.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#F8F8F8")));
        window.setFocusable(true);
        window.setOutsideTouchable(true);

        window.update();

        //設(shè)置顯示位置

        window.showAsDropDown(msgView, 0, 0);//msgView就是我們menu中的btn_msg
        break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

4、在上面3中有一個(gè)布局popupwindow,我項(xiàng)目中用到的時(shí)顯示通知,我在布局中用了ListView來顯示內(nèi)容。這里也可以把布局寫成固定布局,根據(jù)自己的需求充分發(fā)揮。下面貼出來popupwindow布局,如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
  <ListView
    android:id="@+id/lv_msg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:divider="@null"
    android:scrollbars="none" />
</LinearLayout>

5、到這里就實(shí)現(xiàn)了我們想要的功能,結(jié)果圖:

Android怎么實(shí)現(xiàn)仿微信和qq點(diǎn)擊右上角加號(hào)彈出操作框

6、通知和任務(wù)右邊顯示的信息條數(shù),是用的shape  xml文件進(jìn)行約束的,也可以使用BadgeView實(shí)現(xiàn),這里就不過多說明了。

讀到這里,這篇“Android怎么實(shí)現(xiàn)仿微信和qq點(diǎn)擊右上角加號(hào)彈出操作框”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI