1. 程式人生 > >PopupWindow實現微信右上角選單

PopupWindow實現微信右上角選單

蛋疼的各種仿照效果,UI一定要伺候好,不然一言不合就給你來一個很NB的效果

上程式碼!

1.資源圖片

     

2.popupWindow入場動畫

<style name="AnimationPreview">
    <item name="android:windowEnterAnimation">@anim/fade_in</item>
    <item name="android:windowExitAnimation">@anim/fade_out</item>
</style>

3.自定義popupWindow

package com.amistrong.beautifulstar.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.amistrong.beautifulstar.chat.activity.GroupChatActivity;
import com.amistrong.beautifulstar.R;
import com.amistrong.beautifulstar.find.ui.activity.friend.MyFriendActivity;
import com.amistrong.beautifulstar.utils.scancodetool.decoding.Intents;

public class MorePopWindow extends PopupWindow {
    private View conentView;
    private String userId;
    public MorePopWindow(final Activity context) {
        final LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        conentView = inflater.inflate(R.layout.popup_window_chat, null);
        int h = context.getWindowManager().getDefaultDisplay().getHeight();
        int w = context.getWindowManager().getDefaultDisplay().getWidth();
        this.setContentView(conentView);
        this.setWidth(LayoutParams.WRAP_CONTENT);
        this.setHeight(LayoutParams.WRAP_CONTENT);
        //設定點選隱藏的屬性
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.update();
        ColorDrawable dw = new ColorDrawable(0000000000);
        this.setBackgroundDrawable(dw);
        this.setAnimationStyle(R.style.AnimationPreview);
        RelativeLayout getChat = (RelativeLayout) conentView.findViewById(R.id.getChat);
        RelativeLayout getGroup = (RelativeLayout) conentView.findViewById(R.id.getGroup);
        getChat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //點選彈出按鈕的操作
                Intent intent=new Intent(context, MyFriendActivity.class);
                context.startActivity(intent);
                MorePopWindow.this.dismiss();
            }
        });
        getGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, GroupChatActivity.class);
                context.startActivity(intent);
                MorePopWindow.this.dismiss();
            }
        });
    }

    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 10);
        } else {
            this.dismiss();
        }
    }
}


4.xml檔案

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:id="@+id/llPop"
android:layout_height="wrap_content"
android:background="#00000000"
android:orientation="vertical">
    <LinearLayout
android:descendantFocusability="blocksDescendants" android:id="@+id/llChatFragmentChat" android:layout_width="150dp" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/rlChatFragmentTitle" android:layout_marginRight="5dp" android:background="@mipmap/chat_96" android:orientation="vertical"> <RelativeLayout android:layout_marginTop="10dp" android:id="@+id/getChat" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_gravity="center_horizontal" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:orientation="horizontal"> <ImageView android:id="@+id/ivChatOne" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:src="@mipmap/chat_98" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_toRightOf="@+id/ivChatOne" android:text="發起聊天" android:textColor="@android:color/white" android:textSize="@dimen/text_content_15" /> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginBottom="5dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="5dp" android:background="@color/view_background" /> <RelativeLayout android:id="@+id/getGroup" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_gravity="center_horizontal" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:orientation="horizontal"> <ImageView android:id="@+id/ivChatTwo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:src="@mipmap/chat_99" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_toRightOf="@+id/ivChatTwo" android:text="發起群聊" android:textColor="@android:color/white" android:textSize="@dimen/text_content_15" /> </RelativeLayout> </LinearLayout> </LinearLayout>

結束