popupwindow的学习

源地址:http://blog.csdn.net/xyz_lmn/article/details/6906286

自定义的一个弹窗,学习学习。

MainActivity.java:

package com.example.li.popupwindow;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    View view;
    PopupWindow pop;
    Button btnShowAsDrawDown;
    Button btnShowAsDrawDown1;
    Button btnShowAtLocation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popup_activity);
        btnShowAsDrawDown = (Button) findViewById(R.id.btnShowAsDrawDown);
        btnShowAsDrawDown.setOnClickListener(this);
        btnShowAsDrawDown1 = (Button) findViewById(R.id.btnShowAsDrawDown1);
        btnShowAsDrawDown1.setOnClickListener(this);
        btnShowAtLocation = (Button) findViewById(R.id.btnShowAt);
        btnShowAtLocation.setOnClickListener(this);
        initPopupWindow();
    }

    private void initPopupWindow() {
        view = this.getLayoutInflater().inflate(R.layout.popup_window, null);
        pop = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
                );
        pop.setOutsideTouchable(true);
        Button btnpop = (Button) view.findViewById(R.id.btn_pop);
        btnpop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.dismiss();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnShowAsDrawDown:
                if(pop.isShowing()){
                    pop.dismiss();
                }else{
                    pop.showAsDropDown(v);
                }
                break;
            case R.id.btnShowAsDrawDown1:
                if(pop.isShowing()){
                    pop.dismiss();
                }else{
                    pop.showAsDropDown(v, 0, -160);
                }
                break;
            default:
                if(pop.isShowing()){
                    pop.dismiss();
                }else{
                    pop.showAtLocation(findViewById(R.id.main),
                            Gravity.CENTER_HORIZONTAL, 0, 0
                            );
                }
                break;
        }
    }
}

popup_activity.xml:

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="pop demo!"
        />

    <Button
        android:id="@+id/btnShowAsDrawDown"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Show as drawndown(one parameter)" />

    <Button
        android:id="@+id/btnShowAsDrawDown1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Show as drawndown(three parameters)" />

    <Button
        android:id="@+id/btnShowAt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Show At Location" />

</LinearLayout>

popup_window.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="#a00ddddd"
    android:gravity="center"
    >

    <Button
        android:id="@+id/btn_pop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:text="Close"
        />

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值