前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android Popupwindow弹出窗口的简单使用方法

Android Popupwindow弹出窗口的简单使用方法

作者头像
砸漏
发布2020-10-22 11:11:04
2.4K0
发布2020-10-22 11:11:04
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

本文实例为大家分享了Android Popupwindow弹出窗口的具体代码,供大家参考,具体内容如下

代码很简单,没有和别的控件连用。布局自己随意定义,我的这个是最基础的,就直接上代码啦!

在MainActivity里

代码语言:javascript
复制
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
  private Context mContext = null;

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

    mContext = this;

    Button button = (Button) findViewById(R.id.but);
    button.setOnClickListener(newView.OnClickListener() {

      @Override
      public void onClick(View view) {

        showPopupWindow(view);
      }
    });
  }

  private void showPopupWindow(View view) {

    // 一个自定义的布局,作为显示的内容
    View contentView =LayoutInflater.from(mContext).inflate(
        R.layout.popupwindow, null);
    // 设置按钮的点击事件
    Button button = (Button) contentView.findViewById(R.id.button );
      button.setOnClickListener(new View.OnClickListener() {

          @Override
          public void onClick(View v) {
            Toast.makeText(mContext, "button is pressed",
                Toast.LENGTH_SHORT).show();
          }
    });

    final PopupWindow popupWindow = new PopupWindow(contentView,  LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, true);

    // 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
    // 我觉得这里是API的一个bug
    popupWindow.setBackgroundDrawable(getResources().getDrawable(R.mipmap.ic_launcher));

    // 设置好参数之后再show
    popupWindow.showAsDropDown(view);
  }
}

在主布局里

代码语言:javascript
复制
 <Button
    android:id="@+id/but"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" / 

在popupwindow布局里

代码语言:javascript
复制
<?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"
  android:background="#ffff00" 
<TextView
  android:id="@+id/ttt"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textSize="20sp"
  android:text="弹出窗口"/ 
  <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" / 
</LinearLayout 

效果图

以上就是本文的全部内容,希望对大家的学习有所帮助。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档