PopupWindow基本使用方法

代码

public class MainActivity extends Activity implements OnClickListener {

    private Context context = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button bt = (Button) findViewById(R.id.bt);
        bt.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.bt:
            showPopWindow(v);
            break;
        case R.id.layoutSeclect1:
            Toast.makeText(context, "模式1", Toast.LENGTH_SHORT).show();
            break;
        case R.id.layoutSeclect2:
            Toast.makeText(context, "模式2", Toast.LENGTH_SHORT).show();
            break;
        }
    }

    private void showPopWindow(View v) {
        // 一、把一个自定义的布局打气,作为popwindow要显示的内容
        View contentView = LayoutInflater.from(context).inflate(
                R.layout.pop_layout, null);
        // 二、找到该布局中的控件
        LinearLayout layoutSeclect1 = (LinearLayout) contentView
                .findViewById(R.id.layoutSeclect1);
        LinearLayout layoutSeclect2 = (LinearLayout) contentView
                .findViewById(R.id.layoutSeclect2);
        // 三、设置控件的点击事件
        layoutSeclect1.setOnClickListener(this);
        layoutSeclect2.setOnClickListener(this);
        // 四、new出PopupWindow,参数为PopupWindow显示的内容view、宽、高、是否可获取焦点
        PopupWindow popupWindow = new PopupWindow(contentView,
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);// 不写也行,因为默认为true,可触摸
        // 五、★必须设置背景:如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
        popupWindow.setBackgroundDrawable(new BitmapDrawable(context
                .getResources()));
        // ——————————————————————————————————————————————————————————————————————————————————————————
        // 六、让PopupWindow显示出来:显示popwindow的方式有两种,看你是想相对于某个控件显示,还是相对于父控件显示
        // 1.showAsDropDown指的是显示在v控件的周围,是以这个控件为基点的
        // popupWindow.showAsDropDown(v);
        // popupWindow.showAsDropDown(v, 0, -30);//这里的坐标是基于附着控件的左下角,下右为正
        // 2.showAtLocation指的是显示在现对于父控件的位置,第一个参数view和相对于控件的一样
        popupWindow.showAtLocation(v, Gravity.CENTER | Gravity.CENTER, 0, 0);

    }

}

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.mypopwindowdemo.MainActivity" >

    <Button
        android:id="@+id/bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="模式选择" />

</RelativeLayout>

PopupWindow布局

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#0000" >

    <!--
  popupwimdow的布局注意不能使用类似        
    android:layout_marginTop=""这样属性,特别是相对布局时候. 
  布局或者控件整体靠左等都会影响popupwimdow定位使用
    -->

    <LinearLayout
        android:id="@+id/hot_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/pop"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/layoutSeclect1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:onClick="OnclickTestListener" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:text="回家的方案1"
                android:textColor="#fff"
                android:textSize="18sp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/layoutSeclect2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:onClick="OnclickTestListener" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:text="回家的方案2"
                android:textColor="#fff"
                android:textSize="18sp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

showAtLocation()是popupwindow的方法,用于显示相对于父控件的位置的方法。

PopupWindow的位置按照有无偏移分,可以分为偏移和无偏移两种;按照参照物的不同,可以分为相对于某个控件(Anchor锚)和相对于父控件。具体如下
showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移
showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移
showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移


点击下面链接—学习popwindow的动画效果:

setAnimationStyle实现的popwindow显示消失的动画效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值