欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android popupwindow简单使用方法介绍

程序员文章站 2024-02-22 12:20:52
先看下效果 1.首页 package com.yskj.jh.demopopupwindow; import android.content.c...

先看下效果

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>

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