ListPopupWindow

 

宽度一般是控件的宽度: mListPopupWindow.setAnchorView(compoundButton);

  check_down.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                mListPopupWindow.setAnchorView(compoundButton);
                if(b) {
                    mListPopupWindow.show();
                }else{
                    mListPopupWindow.dismiss();
                }
            }
        });

  

 

 private void initDialog(){
        final String itmes[]={"第一个子项","第二个子项","第三个子项"};
        mListPopupWindow=new ListPopupWindow(getActivity());
        mListPopupWindow.setAdapter(new ArrayAdapter<String>(getActivity(),R.layout.print_pop_item, itmes));
        mListPopupWindow.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                Toast.makeText(getActivity(), "点击了"+itmes[position], Toast.LENGTH_SHORT).show();
            }
        });
    }

  

 

print_pop_item.xml

<?xml version="1.0" encoding="utf-8"?>
    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

  

posted @ 2021-09-08 10:36  a318013800  阅读(77)  评论(0编辑  收藏  举报