popupwindow简单使用

popupwindow的简单使用。

主界面布局:main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.example.ningshengcai.popupwindow.MainActivity">

    <Button
        android:id="@+id/popup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/popupwindow"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

自定义popupwindiw 布局:

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

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text"/>
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:text="toast"
        android:layout_height="wrap_content" />
    <ImageButton
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher"/>
</LinearLayout>

主代码:MainActivity.java

package com.example.ningshengcai.popupwindow;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button popupwindow;
    private PopupWindow mpopupwindow;
    boolean flag = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initUI();
    }

    private void initUI() {
        popupwindow = (Button)findViewById(R.id.popup);
        popupwindow.setOnClickListener(new popupwindowListener());
    }
    public class popupwindowListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            if (flag){
                showpopupwindow();//打开popupwindow
                mpopupwindow.showAsDropDown(v);//设置弹出的位置,控件下方
                // mpopupwindow.showAsDropDown(View anchor, int xoff, int yoff)//相对某个控件的位置,有偏移
                flag = false;
            }else if (!flag){
                mpopupwindow.dismiss();//关闭popupwindow
                //更新窗口的位置
               // mpopupwindow.update(300, 300, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                flag=true;
            }

        }
    }

    private void showpopupwindow(){
        View contentView = LayoutInflater.from(this).inflate(R.layout.popup, null);//设置contentview
        mpopupwindow = new PopupWindow(contentView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);//设置线性默认宽高
       // 设置弹出窗体需要软键盘,INPUT_METHOD_FROM_FOCUSABLE, INPUT_METHOD_NEEDED, INPUT_METHOD_NOT_NEEDED.
       // mpopupwindow.setInputMethodMode(PopupWindow.INPUT_METHOD_FROM_FOCUSABLE);
        //mpopupwindow.setWidth(100);//设置窗口宽度
       // mpopupwindow.setHeight(200);//设置窗口高度
        Button btn = (Button)contentView.findViewById(R.id.btn);
        final TextView text = (TextView) contentView.findViewById(R.id.text);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text.setText("toast");
                Toast.makeText(getApplication(),"nsc",Toast.LENGTH_SHORT).show();
            }
        });
    }
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值