Android UI--PopupWindow的使用

2017-10-29  本文已影响55人  倾倒的吞天壶

1. 常用属性及方法简介

1) 属性

很奇怪,文档里提供了相关属性,但popupWindow有相应控件吗?

2) 方法

2. 代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.chenli.popupwindow.MainActivity">

    <TextView
        android:id="@+id/tv_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_marginBottom="2.5dp"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:layout_marginRight="2.5dp"
            android:text="持仓"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Switch
            android:id="@+id/position"
            android:layout_marginLeft="2.5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:layout_marginBottom="2.5dp"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:layout_marginRight="2.5dp"
            android:text="挂单"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Switch
            android:id="@+id/pending"
            android:layout_marginLeft="2.5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:layout_marginRight="2.5dp"
            android:text="均线"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Switch
            android:id="@+id/average_line"
            android:layout_marginLeft="2.5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>


</LinearLayout>
public class MainActivity extends AppCompatActivity {
    private TextView textView;
    private PopupWindow popWindow;
    private Switch position;
    private Switch pending;
    private Switch average;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View view = LayoutInflater.from(this).inflate(R.layout.popup_layout, null, false);
        position = (Switch) view.findViewById(R.id.position);
        pending = (Switch) view.findViewById(R.id.pending);
        average = (Switch) view.findViewById(R.id.average_line);
        //1.构造一个PopupWindow,参数依次是加载的View,宽高
        popWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popWindow.setAnimationStyle(R.style.anim_menu_bottom_bar);

        textView = (TextView) findViewById(R.id.tv_id);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initPopWindow(getWindow().getDecorView());
            }
        });
    }

    private void initPopWindow(View v) {
        //设置popupWindow显示的位置,参数依次是参照View,x轴的偏移量,y轴的偏移量
        popWindow.showAtLocation(v, Gravity.CENTER, 0, 0);

        //设置popupWindow里的按钮的事件
        position.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "你点击了持仓", Toast.LENGTH_SHORT).show();
//                popWindow.dismiss();
            }
        });
        pending.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "你点击了挂单", Toast.LENGTH_SHORT).show();
//                popWindow.dismiss();
            }
        });
        average.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "你点击了均线", Toast.LENGTH_SHORT).show();
//                popWindow.dismiss();
            }
        });
    }
}

3. github

PS:点击空白处的时候让PopupWindow消失

关于PopupWindow最搞笑的地方是setOutsideTouchable方法,原本以为如果你setOutsideTouchable(true)则点击PopupWindow外的地方PopupWindow会消失,其实这玩意儿好像一点用都没有。

要让点击PopupWindow之外的地方PopupWindow消失你需要调用setBackgroundDrawable(new BitmapDrawable());

设置背景,为了不影响样式,这个背景是空的。还可以这样写,觉得这样要保险些:
setBackgroundDrawable(new ColorDrawable(0x00000000));
背景不为空但是完全透明。如此设置还能让PopupWindow在点击back的时候消失。其实一直觉得很奇怪,不明白为什么一个背景会影响点击事件,只知道这样用可行。
详情见:
http://www.cnblogs.com/mengdd/p/3569127.html

上一篇 下一篇

猜你喜欢

热点阅读