android自定義popupwindow仿微信右上角彈出菜單效果
微信右上角的操作菜單看起來(lái)很好用,就照著仿了一下,不過是舊版微信的,手里剛好有一些舊版微信的資源圖標(biāo),給大家分享一下。
不知道微信是用什么實(shí)現(xiàn)的,我使用popupwindow來(lái)實(shí)現(xiàn),主要分為幾塊內(nèi)容:
1、窗口布局文件:popwin_share.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/title_tools_bg"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/layout_share"
style="@style/fill_width"
android:orientation="horizontal"
android:background="@drawable/menu_left_item_selector"
android:padding="5dp">
<ImageView
android:layout_marginLeft="7dp"
style="@style/wrap"
android:scaleType="fitCenter"
android:src="@drawable/share" />
<TextView
style="@style/wrap"
android:textColor="@color/white"
android:textSize="@dimen/text18"
android:layout_marginLeft="5dp"
android:text="分享內(nèi)容" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout_copy"
style="@style/fill_width"
android:orientation="horizontal"
android:background="@drawable/menu_left_item_selector"
android:padding="5dp">
<ImageView
android:layout_marginLeft="5dp"
style="@style/wrap"
android:scaleType="fitCenter"
android:src="@drawable/copy_pressed" />
<TextView
style="@style/wrap"
android:textColor="@color/white"
android:textSize="@dimen/text18"
android:layout_marginLeft="5dp"
android:text="復(fù)制結(jié)果" />
</LinearLayout>
</LinearLayout>
采用線性布局,因?yàn)槔锩媸且恍幸恍胸Q排的菜單,線性布局更容易控制。大布局里面放了兩個(gè)垂直排列的線性布局,每個(gè)線性布局中分別有橫向排列的imageview和textview,很簡(jiǎn)單的布局。大布局的背景用了一個(gè)圖片,當(dāng)然也可以自定義一些其他顏色。
2、popupwindow代碼,我這里是自定義一個(gè)popupwindows類,繼承自PopupWindow:
package com.xjw.view;
import com.xjw.translate.R;
import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
/**
* 項(xiàng)目名稱:translate
* 實(shí)現(xiàn)功能: 翻譯詳情界面,分享彈出窗口
* 類名稱:PopWinShare
* 類描述:(該類的主要功能)
* 創(chuàng)建人:徐紀(jì)偉
* E-mail: xujiwei558@126.com
* 創(chuàng)建時(shí)間:2014年10月18日 下午4:37:25
* @version
*/
public class PopWinShare extends PopupWindow{
private View mainView;
private LinearLayout layoutShare, layoutCopy;
public PopWinShare(Activity paramActivity, View.OnClickListener paramOnClickListener, int paramInt1, int paramInt2){
super(paramActivity);
//窗口布局
mainView = LayoutInflater.from(paramActivity).inflate(R.layout.popwin_share, null);
//分享布局
layoutShare = ((LinearLayout)mainView.findViewById(R.id.layout_share));
//復(fù)制布局
layoutCopy = (LinearLayout)mainView.findViewById(R.id.layout_copy);
//設(shè)置每個(gè)子布局的事件監(jiān)聽器
if (paramOnClickListener != null){
layoutShare.setOnClickListener(paramOnClickListener);
layoutCopy.setOnClickListener(paramOnClickListener);
}
setContentView(mainView);
//設(shè)置寬度
setWidth(paramInt1);
//設(shè)置高度
setHeight(paramInt2);
//設(shè)置顯示隱藏動(dòng)畫
setAnimationStyle(R.style.AnimTools);
//設(shè)置背景透明
setBackgroundDrawable(new ColorDrawable(0));
}
}
里面提供了一個(gè)構(gòu)造方法,包含四個(gè)參數(shù),第一個(gè)參數(shù)是上下文的activity,第二個(gè)是菜單的點(diǎn)擊事件,從外邊傳遞進(jìn)來(lái)的,要綁定給每一行的菜單,具體的事件實(shí)現(xiàn)當(dāng)然要寫在activity中,后面兩個(gè)分別是彈出窗口的寬度和高度。里面還包含了一個(gè)動(dòng)畫樣式,窗口打開和關(guān)閉時(shí)出現(xiàn)動(dòng)畫的樣式。
3、動(dòng)畫樣式,顯示動(dòng)畫,縮放動(dòng)畫:push_in.xml
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXScale="1.0"
android:toXScale="1.0"
android:fromYScale="0"
android:toYScale="1.0"
android:pivotX="0"
android:pivotY="10%"
android:duration="200" />
關(guān)閉動(dòng)畫,也是縮放動(dòng)畫:push_out.xml
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXScale="1.0"
android:toXScale="1.0"
android:fromYScale="1.0"
android:toYScale="0"
android:pivotX="0"
android:pivotY="10%"
android:duration="200" />
style樣式定義:
<style name="AnimTools" parent="@android:style/Animation"> <item name="android:windowEnterAnimation">@anim/push_in</item> <item name="android:windowExitAnimation">@anim/push_out</item> </style>
到此為止我們的自定義窗口已經(jīng)定義好了。接下來(lái)看使用。
if (popWinShare == null) {
//自定義的單擊事件
OnClickLintener paramOnClickListener = new OnClickLintener();
popWinShare = new PopWinShare(TranslateDataContentActivity.this, paramOnClickListener, DisplayUtil.dip2px(context, 160), DisplayUtil.dip2px(context, 160));
//監(jiān)聽窗口的焦點(diǎn)事件,點(diǎn)擊窗口外面則取消顯示
popWinShare.getContentView().setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
popWinShare.dismiss();
}
}
});
}
//設(shè)置默認(rèn)獲取焦點(diǎn)
popWinShare.setFocusable(true);
//以某個(gè)控件的x和y的偏移量位置開始顯示窗口
popWinShare.showAsDropDown(btnTools, 0, 0);
//如果窗口存在,則更新
popWinShare.update();
每個(gè)子菜單的單擊事件自定義內(nèi)部類,在里面就可以寫每個(gè)子菜單的單擊事件啦,
class OnClickLintener implements OnClickListener{
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.layout_share:
break;
case R.id.layout_copy:
break;
default:
break;
}
}
}
效果預(yù)覽:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android仿微信界面的導(dǎo)航以及右上角菜單欄效果
- Android開發(fā)Popwindow仿微信右上角下拉菜單實(shí)例代碼
- Android仿微信底部菜單欄效果
- Android開發(fā)之微信底部菜單欄實(shí)現(xiàn)的幾種方法匯總
- Android仿微信頂/底部菜單欄效果
- Android仿微信底部菜單欄功能顯示未讀消息數(shù)量
- Android仿微信滑動(dòng)彈出編輯、刪除菜單效果、增加下拉刷新功能
- Android自定義PopWindow實(shí)現(xiàn)QQ、微信彈出菜單效果
- Android制作微信app頂部menu菜單(ActionBar)
- Android實(shí)現(xiàn)微信加號(hào)菜單模式
相關(guān)文章
Android仿微信Viewpager-Fragment惰性加載(lazy-loading)
這篇文章主要為大家詳細(xì)介紹了Android仿微信Viewpager-Fragment惰性加載lazy-loading,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android API開發(fā)之SMS短信服務(wù)處理和獲取聯(lián)系人的方法
這篇文章主要介紹了Android API開發(fā)之SMS短信服務(wù)處理和獲取聯(lián)系人的方法,結(jié)合實(shí)例形式分析了Android API實(shí)現(xiàn)SMS短信發(fā)送及獲取聯(lián)系人的相關(guān)操作步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-08-08
輕松實(shí)現(xiàn)安卓(Android)九宮格解鎖
在平常使用手機(jī)的過程中,九宮格解鎖是我們經(jīng)常接觸到的。常見的比如有鎖屏中的九宮格,還有支付寶中的九宮格等。因?yàn)榫艑m格可以保護(hù)用戶的隱私,所以它的應(yīng)用面很廣泛。那么今天我們就來(lái)自定義一個(gè)屬于自己的九宮格吧!2016-08-08
Android手勢(shì)密碼view學(xué)習(xí)筆記(二)
這篇文章主要為大家詳細(xì)介紹了Android手勢(shì)密碼view的第二篇學(xué)習(xí)筆記,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
給Android初學(xué)者的Gradle知識(shí)普及
剛學(xué) Android 不久,對(duì) Gradle 不懂,看了很多資料依然一知半解,很多人都這樣覺得,表示同感,下面小編來(lái)給大家講講 Gradle相關(guān)知識(shí),需要的朋友跟隨小編一起來(lái)學(xué)習(xí)一下2018-09-09
解決android Listview的item中最外層Margin失效的問題
下面小編就為大家?guī)?lái)一篇解決android Listview的item中最外層Margin失效的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-04-04
Android 網(wǎng)絡(luò)圖片查看顯示的實(shí)現(xiàn)方法
本篇文章小編為大家介紹,Android 網(wǎng)絡(luò)圖片查看顯示的實(shí)現(xiàn)方法,需要的朋友參考下2013-04-04
android自定義toast(widget開發(fā))示例
這篇文章主要介紹了android自定義toast(widget開發(fā))示例,需要的朋友可以參考下2014-03-03

