Android PopupWindow 动态更新宽度

package com.inks.UI;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by inks on 2017/10/21 0021.
 */

public class PromptPopupWindow {
    private LayoutInflater inflater;
    private PopupWindow pWindow;
    private Context context;
    private View contentView = null;
    private Window window;
    TextView textView;
    public void miss() {
        if (pWindow != null && pWindow.isShowing()) {
            pWindow.dismiss();
        }

    }

    protected void popupPrompt(View v, LayoutInflater inflater, String prompt, int time) {

        if (pWindow != null && pWindow.isShowing()) {
            textView.setText(prompt);
            contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
            pWindow.update( contentView.getMeasuredWidth(),-2);
            myHandler.removeMessages(1);
            myHandler.sendEmptyMessageDelayed(1, time);
        }else{
            contentView = inflater.inflate(R.layout.popup_prompt, null);
            textView = contentView.findViewById(R.id.prompt);
            textView.setText(prompt);
            contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
            pWindow = new PopupWindow(contentView, contentView.getMeasuredWidth(), -2);
            // backgroundAlpha(0.5f);
            pWindow.setAnimationStyle(R.style.popupwin_style);
            pWindow.setFocusable(false);
            pWindow.setOutsideTouchable(true);
            pWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
            pWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
            pWindow.showAtLocation(v, Gravity.TOP, 0, 100);
            myHandler.removeMessages(1);
            myHandler.sendEmptyMessageDelayed(1, time);
            pWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    // backgroundAlpha(1f);
                }
            });
        }




    }

    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.alpha = bgAlpha; // 0.0-1.0
        window.setAttributes(lp);
    }

    Handler myHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    miss();
                    break;
            }
        }
    };

}
 
  

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:elevation="5dp"
    android:orientation="horizontal"
    >

            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/im_border_prompt_bg1">

                    android:id="@+id/prompt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/im_border_prompt_bg2"
            android:paddingLeft="150dp"
            android:paddingRight="150dp"
            android:paddingTop="20dp"
            android:paddingBottom="20dp"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="5dp"
            android:maxLines="1"
            android:textSize="36dp"
            android:gravity="center"
            android:textColor="#ffffff"
            android:text="这是一个提示这是一个提示"/>

    









你可能感兴趣的:(Android PopupWindow 动态更新宽度)