仿QQ右上角彈出窗pop

  • 1.Activity簡單布局

效果圖省略,下面是activity內(nèi)的布局文件 R.layout.main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingTop="@dimen/dp_24">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_60"
        android:background="@color/white"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <ImageButton
            android:id="@+id/img_btn_back"
            android:layout_width="@dimen/dp_45"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/dp_10"
            android:background="@color/transparent"
            android:src="@drawable/ic_back" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/dp_20"
            android:layout_weight="1"
            android:text="@string/fd"
            android:textColor="@color/black_font"
            android:textSize="@dimen/sp_18" />

        <ImageButton
            android:id="@+id/img_btn_add_f"
            android:layout_width="45dp"
            android:layout_height="match_parent"
            android:layout_marginRight="@dimen/dp_10"
            android:background="@color/transparent"
            android:src="@drawable/ic_friend_more" />


    </LinearLayout>
</LinearLayout>
  • 2.彈出窗布局

創(chuàng)建布局文件R.layout.dialog_friend_more.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/friend_pop_bg"
   android:gravity="center"
   >

   <LinearLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:orientation="vertical"
       >

       <TextView
           android:id="@+id/new_friend"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:drawableLeft="@drawable/ic_add_friend"
           android:drawablePadding="@dimen/dp_15"
           android:gravity="center"
           android:padding="10dp"
           android:text="新朋友"
           android:textColor="@color/colorGray"/>

       <View
           android:layout_width="match_parent"
           android:layout_height="1dp"
           android:background="#ECECEC"/>

       <TextView
           android:id="@+id/back_friend"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:drawableLeft="@drawable/ic_black"
           android:drawablePadding="@dimen/dp_15"
           android:gravity="center"
           android:padding="10dp"
           android:text="黑名單"
           android:textColor="@color/colorGray"/>
   </LinearLayout>
</LinearLayout>
  • 3.動(dòng)畫布局

在res的anim文件夾下創(chuàng)建pop_add_show.xml和pop_add_hide.xml

   <style name="pop_add">
        <item name="android:windowEnterAnimation">@anim/pop_add_show</item>
        <item name="android:windowExitAnimation">@anim/pop_add_hide</item>
    </style>

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="@integer/config_pop_duration"
        android:fromAlpha="0.0"
        android:toAlpha="1.0"/>
    <scale
        android:duration="@integer/config_pop_duration"
        android:fromXScale="0"
        android:fromYScale="0"
        android:interpolator="@android:anim/decelerate_interpolator"
        android:pivotX="85%"
        android:pivotY="0%"
        android:toXScale="1.0"
        android:toYScale="1.0"/>

</set>

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="@integer/config_pop_duration"
        android:fromAlpha="1.0"
        android:toAlpha="0.0"/>
    <scale
        android:duration="@integer/config_pop_duration"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:pivotX="85%"
        android:pivotY="0%"
        android:toXScale="0"
        android:toYScale="0"/>
</set>
  • 4.使用


    private void showPop() {
        PopupWindow mPopupWindow = new PopupWindow(this);
        // 設(shè)置布局文件
        View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_friend_more, null);
        mPopupWindow.setContentView(contentView);
        // 為了避免部分機(jī)型不顯示嫂侍,我們需要重新設(shè)置一下寬高
        mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 設(shè)置pop透明效果
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(0x0000));
        // 設(shè)置pop出入動(dòng)畫
        mPopupWindow.setAnimationStyle(R.style.pop_add);
        // 設(shè)置pop獲取焦點(diǎn)驱证,如果為false點(diǎn)擊返回按鈕會(huì)退出當(dāng)前Activity雕拼,如果pop中有Editor的話,focusable必須要為true
        mPopupWindow.setFocusable(true);
        // 設(shè)置pop可點(diǎn)擊歼捏,為false點(diǎn)擊事件無效,默認(rèn)為true
        mPopupWindow.setTouchable(true);
        // 設(shè)置點(diǎn)擊pop外側(cè)消失笨篷,默認(rèn)為false瞳秽;在focusable為true時(shí)點(diǎn)擊外側(cè)始終消失
        mPopupWindow.setOutsideTouchable(true);
        // 相對(duì)于 + 號(hào)正下面,同時(shí)可以設(shè)置偏移量
        mPopupWindow.showAsDropDown(imgBtnAddF, -100, 0);
        backgroundAlpha(0.7f);
        // 設(shè)置pop關(guān)閉監(jiān)聽率翅,用于改變背景透明度
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1.0f);
            }
        });
        mPopupWindow.getContentView().findViewById(R.id.new_friend).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mContext, FriendRequireListActivity.class));
                mPopupWindow.dismiss();
            }
        });
        mPopupWindow.getContentView().findViewById(R.id.back_friend).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mContext, FriendBlackListActivity.class));
                mPopupWindow.dismiss();
            }
        });
    }

  /**
     * 此方法用于改變背景的透明度练俐,從而達(dá)到“變暗”的效果
     */
    private void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        // 0.0-1.0
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);
        // everything behind this window will be dimmed.
        // 此方法用來設(shè)置浮動(dòng)層,防止部分手機(jī)變暗無效
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末冕臭,一起剝皮案震驚了整個(gè)濱河市腺晾,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌辜贵,老刑警劉巖悯蝉,帶你破解...
    沈念sama閱讀 222,000評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異托慨,居然都是意外死亡鼻由,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,745評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來嗡靡,“玉大人跺撼,你說我怎么就攤上這事√直耍” “怎么了歉井?”我有些...
    開封第一講書人閱讀 168,561評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長哈误。 經(jīng)常有香客問我哩至,道長,這世上最難降的妖魔是什么蜜自? 我笑而不...
    開封第一講書人閱讀 59,782評(píng)論 1 298
  • 正文 為了忘掉前任菩貌,我火速辦了婚禮,結(jié)果婚禮上重荠,老公的妹妹穿的比我還像新娘箭阶。我一直安慰自己,他們只是感情好戈鲁,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,798評(píng)論 6 397
  • 文/花漫 我一把揭開白布仇参。 她就那樣靜靜地躺著,像睡著了一般婆殿。 火紅的嫁衣襯著肌膚如雪诈乒。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,394評(píng)論 1 310
  • 那天婆芦,我揣著相機(jī)與錄音怕磨,去河邊找鬼。 笑死消约,一個(gè)胖子當(dāng)著我的面吹牛肠鲫,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播或粮,決...
    沈念sama閱讀 40,952評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼导饲,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼!你這毒婦竟也來了被啼?” 一聲冷哼從身側(cè)響起帜消,我...
    開封第一講書人閱讀 39,852評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎浓体,沒想到半個(gè)月后泡挺,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,409評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡命浴,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,483評(píng)論 3 341
  • 正文 我和宋清朗相戀三年娄猫,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了贱除。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,615評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡媳溺,死狀恐怖月幌,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情悬蔽,我是刑警寧澤扯躺,帶...
    沈念sama閱讀 36,303評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站蝎困,受9級(jí)特大地震影響录语,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜禾乘,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,979評(píng)論 3 334
  • 文/蒙蒙 一澎埠、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧始藕,春花似錦蒲稳、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,470評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至拙已,卻和暖如春决记,著一層夾襖步出監(jiān)牢的瞬間摧冀,已是汗流浹背倍踪。 一陣腳步聲響...
    開封第一講書人閱讀 33,571評(píng)論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留索昂,地道東北人建车。 一個(gè)月前我還...
    沈念sama閱讀 49,041評(píng)論 3 377
  • 正文 我出身青樓,卻偏偏與公主長得像椒惨,于是被迫代替她去往敵國和親缤至。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,630評(píng)論 2 359