先看下效果 @H_404_1@
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,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.setoutsIDe@R_419_5985@able(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 popupwindow简单使用方法介绍全部内容,希望文章能够帮你解决Android popupwindow简单使用方法介绍所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)