androidUI——PopWindow

3个文件,MainActivity.java,activity_main.xml,popup_window.xml
MainActivity:

package com.example.popwindowdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;
/**
 * 
 * @author H100
 * PopWindow和AlertDialogDialog的区别
 * Alertdialog居中显示,PopWindow不居中显示,显示在指定位置
 * AlertDialog会阻塞线程,比如Ui,PopWindow不会
 *
 */
public class MainActivity extends Activity implements OnClickListener{
    Button btn_one,btn_three,btn_at;
    PopupWindow pop;

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

    void initButton(){
        btn_one=(Button) findViewById(R.id.btnShowAsDrawDown);
        btn_three=(Button) findViewById(R.id.btnShowAsDrawDown1);
        btn_at=(Button) findViewById(R.id.btnShowAt);
        btn_at.setOnClickListener(this);
        btn_one.setOnClickListener(this);
        btn_three.setOnClickListener(this);
    }
    void initPopwindow(){
        View view=LayoutInflater.from(this).inflate(R.layout.popup_window, null);
        /**
         * 关于layoutParams的设置方法一般就是设置父控件的LayoutParams,下面的竟然也能用,也就是父父的也可以
         */
        pop=new PopupWindow(view, LinearLayout.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        pop.setOutsideTouchable(true);
        Button btn=(Button) view.findViewById(R.id.btn_pop);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                pop.dismiss();
                Toast.makeText(MainActivity.this, "popwindow手动退出", 500).show();
            }
        });
    }
    @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,-320);//起始点向上平移320
            }
            break;
        case R.id.btnShowAt:
            if(pop.isShowing()){
                pop.dismiss();
            }else{
                //为了让这个水平平移效果不被FillParent遮盖就干脆设置成Wrap
                pop.setWindowLayoutMode(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                pop.showAtLocation(findViewById(R.id.main), Gravity.CENTER_HORIZONTAL, 100, 50);
            }
            break;

        default:
            break;
        }

    }


}

activity_main.xml:

<LinearLayout 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:id="@+id/main"
    android:orientation="vertical"
    tools:context="com.example.popwindowdemo.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/btnShowAsDrawDown"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="show as drawdown(one parameter)" />

    <Button
        android:id="@+id/btnShowAsDrawDown1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="show as drawdown(three parameter)" />

    <Button
        android:id="@+id/btnShowAt"
        android:layout_width="fill_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="vertical" 
    android:background="#ff9aaab7">
    <!-- 突出显示所以设置颜色要不就透明了 -->
    <Button android:id="@+id/btn_pop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="dismiss"/>
    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="none"/>

</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值