Android popupwindow简单使用方法介绍

 更新时间:2016年12月29日 15:01:05   作者:JH_Manny  
这篇文章主要为大家详细介绍了Android popupwindow简单使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

先看下效果

1.首页

package com.yskj.jh.demopopupwindow;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
 private Button button;
 private PopupWindow kindsPopupWindow;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  button = (Button) findViewById(R.id.button);
  button.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    getKindPopupWindow();
   }
  });
 }

 private void getKindPopupWindow() {
  final ArrayList kindsList = new ArrayList();
  kindsList.add("全部分类");
  kindsList.add("今日上线");
  kindsList.add("美食");
  kindsList.add("酒店");
  kindsList.add("旅游");
  LayoutInflater inflater = LayoutInflater.from(this);
  // 引入窗口配置文件
  View view = inflater.inflate(R.layout.pop_local_kind, null);
  // 创建PopupWindow对象
  kindsPopupWindow = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, false);
  //设置popupWindow宽
  kindsPopupWindow.setWidth(button.getWidth());
  ListView listView = (ListView) view.findViewById(R.id.list);
  PopAdapter adapter = new PopAdapter(MainActivity.this,kindsList);
  listView.setAdapter(adapter);

  // 需要设置一下此参数,点击外边可消失
  kindsPopupWindow.setBackgroundDrawable(new BitmapDrawable());
  //设置点击窗口外边窗口消失
  kindsPopupWindow.setOutsideTouchable(true);
  // 设置此参数获得焦点,否则无法点击
  kindsPopupWindow.setFocusable(true);
  //设置popupWindow显示位置
  kindsPopupWindow.showAsDropDown(button);
  listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    Toast.makeText(MainActivity.this,i+"pop",Toast.LENGTH_SHORT).show();
    button.setText(kindsList.get(i).toString());
    kindsPopupWindow.dismiss();
   }
  });
 }

 //pop适配器
 private class PopAdapter extends BaseAdapter {
  private Context context;
  private ArrayList<String> list;

  public PopAdapter(Context context, ArrayList<String> list) {
   this.context = context;
   this.list = list;
  }

  @Override
  public int getCount() {
   if (list==null||list.size()==0){
    return 0;
   }
   return list.size();
  }

  @Override
  public Object getItem(int i) {
   return list.get(i);
  }

  @Override
  public long getItemId(int i) {
   return i;
  }

  @Override
  public View getView(int i, View convertView, ViewGroup viewGroup) {
   ViewHolder holder = null;
   if(convertView==null){
    holder = new ViewHolder();
    convertView = LayoutInflater.from(context).inflate(R.layout.item_pop_local_kind, null);
    holder.textView = (TextView) convertView.findViewById(R.id.item_text);
    convertView.setTag(holder);
   }else{
    holder=(ViewHolder)convertView.getTag();
   }
   holder.textView.setText(list.get(i).toString());
   return convertView;
  }
  private class ViewHolder {
   TextView textView;
  }
 }
}

2.首页布局

<?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"
 >

 <Button
  android:id="@+id/button"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Show PopupWindow" />
</LinearLayout>

3.popupwindow布局,可根据情况自行布局,这里是demo布局

<?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="#ffffff">

 <ListView
  android:id="@+id/list"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="#888888">
 </ListView>
</LinearLayout>

4.popupwindow条目布局

<?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="@color/white">
 <TextView
  android:id="@+id/item_text"
  android:layout_width="match_parent"
  android:layout_height="40dp"
  android:gravity="center"
  android:text="pop"
  android:textColor="#f08e1f"
  android:background="#eeeeee"
  />
</LinearLayout>

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

相关文章

  • Android使用ShareSDK实现应用分享的功能

    Android使用ShareSDK实现应用分享的功能

    这篇文章主要为大家详细介绍了Android使用ShareSDK实现应用分享的功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • Android用PopupWindow实现自定义overflow

    Android用PopupWindow实现自定义overflow

    这篇文章主要介绍了Android用PopupWindow实现自定义overflow的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • Android WebView软键盘遮挡输入框方案详解

    Android WebView软键盘遮挡输入框方案详解

    这篇文章主要介绍了Android WebView软键盘遮挡输入框方案详解,本文提供了一种新的解决 WebView 输入框被软键盘遮挡的思路,不过这种思路也有它的局限性,目前来看仅适用于全屏的 WebView 中,需要的朋友可以参考下
    2022-06-06
  • Android 使用ViewPager自动滚动循环轮播效果

    Android 使用ViewPager自动滚动循环轮播效果

    本文主要给大家介绍viewpager自动播放,循环滚动的效果,对android viewpager滚动相关知识感兴趣的朋友可以参考下本篇文章
    2015-11-11
  • andoid打包短信发送到gmail邮箱实现代码

    andoid打包短信发送到gmail邮箱实现代码

    andriod短信整合备份发送到gmail邮箱,需要在andoid手机配置好gmail邮箱,下面是具体的实现代码,感兴趣的朋友可以参考下哈
    2013-06-06
  • 详解Android轻量型数据库SQLite

    详解Android轻量型数据库SQLite

    这篇文章主要为大家详细介绍了Android轻量型数据库SQLite,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • Android 使用registerReceiver注册BroadcastReceiver案例详解

    Android 使用registerReceiver注册BroadcastReceiver案例详解

    这篇文章主要介绍了Android 使用registerReceiver注册BroadcastReceiver案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • Android中实现TCP和UDP传输实例

    Android中实现TCP和UDP传输实例

    这篇文章主要介绍了Android中实现TCP和UDP传输实例,本文给出了TCP服务器端代码、TCP客户端代码、UDP服务器端代码、UDP客户端代码等代码实例,需要的朋友可以参考下
    2015-03-03
  • Android性能优化之图片大小,尺寸压缩综合解决方案

    Android性能优化之图片大小,尺寸压缩综合解决方案

    随着Android手机的越来越先进,给我们开发者而言传递的图片也是越来越大,这个时候我们可以对一些没有必要原图展示的图片进行压缩,这篇文章主要给大家介绍了关于Android性能优化之图片大小,尺寸压缩的综合解决方案,需要的朋友可以参考下
    2022-04-04
  • Android Material加载进度条制作代码

    Android Material加载进度条制作代码

    这篇文章主要为大家详细介绍了AndroidMaterial加载进度条的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01

最新评论