PopupWindow+ScrollView的使用(可滚动的对话框)

  1. 1.PopupWIndow的基本用法
  2. ※四个构造函数
  3. //方法一:
    PopupWindow (Context context)
    //方法二:
    PopupWindow(View contentView)
    //方法三:
    PopupWindow(View contentView, int width, int height)
    //方法四:
    PopupWindow(View contentView, int width, int height, boolean focusable)

  4. ※必须具备为PopupWindow设置contentView,width,height,不然不能显示PopupWindow窗口,具体的的设置方法除了上面的构造函数外还可以用setContentView,setWidth,setHeight函数来设置。

  5. 之后就可以使用下面三个函数来显示PopupWindow窗口
    //相对某个控件的位置(正左下方),无偏移  
    showAsDropDown(View anchor)
    //相对某个控件的位置,有偏移;xoff表示x轴的偏移,正值表示向左,负值表示向右;yoff表示相对y轴的偏移,正值是向下,负值是向上;  
    showAsDropDown(View anchor, int xoff, int yoff)
    //相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移  
    showAtLocation(View parent, int gravity, int x, int y)

  6. ※如果想要让PopupWindow窗口在你点击窗口外或back返回键时消失的话,则要设置下面两个函数
  7. popupWindow.setBackgroundDrawable(new BitmapDrawable());//设置背景
    popupWindow.setFocusable(true);//设置窗口的焦点

  8. ※值得注意的是,当在PopupWindow对应的Layout布局中使用ScrollView布局时,可以设置可滚动的很有趣的一个对话框,这点用AlertDialog就做不到。因为AlertDialog的显示位置是固定的了
    
  9. 
    
  10. 另外setAnimationType函数可以设置PopupWindow窗口的进出动画,还有setOutsideTouchable函数可以设置PopupWindow窗口外是否允许点击,isShowing函数可以返回Boolean类型判断是否正在显示PopupWindow窗口,isTouchable函数可以返回Boolean类型判断PopupWindow窗口是否接收点击

  11. 2.可滚动的对话框实现
  12. PopupWindow的布局文件
  13. xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="#60000000">
        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="match_parent">
            <LinearLayout
                android:orientation="vertical"
                android:layout_margin="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
    
                <TextView
                    android:id="@+id/t1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:background="@color/colorPrimary"
                    android:drawableTop="@android:drawable/btn_star_big_on"
                    android:gravity="center"
                    android:text="1"
                    android:textColor="@android:color/white"
                    android:textSize="20dp" />
    
                <View
                    android:layout_width="match_parent"
                    android:layout_height="3dp"/>
    
         /****这里有重复的很多个TextView和View*******/
            LinearLayout>
        ScrollView>
    RelativeLayout>
    MainActivity代码
  14. public class MainActivity extends AppCompatActivity implements View.OnClickListener{
        @BindView(R.id.t)
        TextView textView;
        PopupWindow popupWindow;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ButterKnife.bind(this);
            popupWindow=new PopupWindow(this);
            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            //popupWindow.setOutsideTouchable(true);
            popupWindow.setFocusable(true);
            popupWindow.setAnimationStyle(R.style.animation);
            textView.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.t:
                    View contentView= LayoutInflater.from(this).inflate(R.layout.popup_layout,null);
                    popupWindow.setContentView(contentView);
                    popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
                    popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
                    popupWindow.showAsDropDown(textView);
                    break;
            }
        }
    }
  15. 非常的简单,也就是在PopupWindow的使用的基础上,在布局中添加上ScrollView就可以实现滚动的对话框了。

你可能感兴趣的:(PopupWindow+ScrollView的使用(可滚动的对话框))