设置PopupWindow的背景色

很多时候我们需要自己设置PopupWindoew的背景颜色,怎么做呢?直接在一工具类中写个公共方法,就可以随时引用了。

1.我的是:

public class TipsUtil {
    
    //PopupWindow的背景
    public static void setBackgroundAlpha(AppCompatActivity activity, float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        (activity).getWindow().setAttributes(lp);
    }
    
}


2.在需要的地方引用就行了:

TipsUtil.setBackgroundAlpha(AddPeriodical.this, 1.0f);

3.(实例)比如我的popupWindow:

public void showClassifyPopupWindow() {
    View contentView = LayoutInflater.from(AddPeriodical.this).inflate(R.layout.list_veiw, null);
    ListView listView = contentView.findViewById(R.id.all_list_view);
    //添加用户的所有分类
    loadAllBooksClassify();
    //itme的点击事件
    listView.setOnItemClickListener(new ItemClickListener());
    //创建PopupWindow
    popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, 500);
    popupWindow.setBackgroundDrawable(new ColorDrawable());
    popupWindow.setOutsideTouchable(true);//点击外部消失
    popupWindow.setTouchable(true); //设置可以点击
    popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);//进入退出的动画
    popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {//监听退出
        @Override
        public void onDismiss() {
            popupWindow.dismiss();
        }
    });
    TipsUtil.setBackgroundAlpha(AddPeriodical.this, 0.6f);
    // 按下android回退物理键 PopipWindow消失解决
    listView.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                    return true;
                }
                TipsUtil.setBackgroundAlpha(AddPeriodical.this, 1.0f);
            }
            return false;
        }
    });

    popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
            TipsUtil.setBackgroundAlpha(AddPeriodical.this, 1.0f);
        }
    });
}
//listView点击item项的监听
public class ItemClickListener implements AdapterView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        GetBookMarkTypeAll.DataBean bean = (GetBookMarkTypeAll.DataBean) arg0.getAdapter().getItem(position);
        int id = bean.getId();//类型id
        String classifyNick = bean.getTypeName();//类型昵称
        int lenght = ((GetBookMarkTypeAll.DataBean) arg0.getAdapter().getItem(position)).getCount();
        //根据分类id获取文章
        getData(id);//添加到分类内容中
        tittle.setText(classifyNick + " · " + lenght);
        popupWindow.dismiss();
        TipsUtil.setBackgroundAlpha(AddPeriodical.this, 1.0f);
    }
}


4.我的效果图:

5.我的PopupWindow使用的地方:

showClassifyPopupWindow();
popupWindow.showAtLocation(relativeLayout, Gravity.BOTTOM, 0, 0);//relativeLayout是你需要显示的Activity的根布局

6.我的跟布局:

@BindView(R.id.add_rl)
RelativeLayout relativeLayout;
@BindView(R.id.add_null)
TextView addNull;
@BindView(R.id.add_periodical)
ListView addPeriodical;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/add_rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">

    <include
        android:id="@+id/add_head"
        layout="@layout/layout_title" />

    <TextView
        android:id="@+id/add_null"
        style="@style/maidan"
        android:layout_centerInParent="true"
        android:text="您暂时没有收藏的文章~~"
        android:visibility="gone" />

    <ListView
        android:id="@+id/add_periodical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/add_food"
        android:layout_below="@id/add_head"
        android:descendantFocusability="blocksDescendants"></ListView>
    
</RelativeLayout>


猜你喜欢

转载自blog.csdn.net/qq_41873558/article/details/80641331