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

Android组件popupwindow使用方法详解

程序员文章站 2024-02-28 09:34:10
先看效果:  现在很多的应用效果都需要做的炫些,像uc,以及天天静听,效果很炫的,源码已经对外开放了,有兴趣的可以去研究下的  上源码 mai...

先看效果: 

Android组件popupwindow使用方法详解

现在很多的应用效果都需要做的炫些,像uc,以及天天静听,效果很炫的,源码已经对外开放了,有兴趣的可以去研究下的 

上源码

main.xml

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="500dp"
 android:orientation="vertical" 
 android:background="@android :color/white"
 >

 <include android:id="@+id/top_title"
 
 layout="@layout/title_two_button"/>
 <button 
 android:id="@+id/bb"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="button"
 />
</linearlayout>

title_two_button.xml

<?xml version="1.0" encoding="utf-8"?>
<framelayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content" >

 <textview
 android:id="@+id/tvtitle"
 android:layout_width="fill_parent"
 android:layout_height="45dip"
 android:background="@drawable/search_bg"
 android:gravity="center"
 android:text="天下第一"
 android:textcolor="@color/black"
 android:textsize="25dip" />

 <button
 android:id="@+id/btn_title_left"
 android:layout_width="49dip"
 android:layout_height="36dip"
 android:layout_gravity="center_vertical"
 android:background="@drawable/title_button"
 android:textcolor="@color/white" />

 <button
 android:id="@+id/btn_title_right"
 android:layout_width="49dip"
 android:layout_height="36dip"
 android:layout_gravity="right"
 android:layout_marginright="2dip"
 android:layout_margintop="4dip"
 android:background="@drawable/title_button2"
 android:textcolor="@color/white" />

</framelayout>

group_list.xml

<?xml version="1.0" encoding="utf-8"?>
<linearlayout android:orientation="vertical" android:paddingleft="0.0sp" android:paddingright="0.0sp" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margin="0.0px"
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:background="@drawable/group_bg">

 <imageview 
 android:id="@+id/iv_group_list_bg_divider" 
 android:background="@drawable/group_divider" 
 android:padding="0.0px" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:layout_margin="0.0px" />
 <listview 
 android:id="@+id/lvgroup" 
 android:fadingedgelength="0.0sp" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:listselector="@drawable/grouplist_item_bg" 
 android:drawselectorontop="true" 
 android:cachecolorhint="#00000000" 
 android:divider="@drawable/group_divider" 
 android:dividerheight="2.0px" 
 android:layout_weight="0.0" />
</linearlayout>

group_item.xml

<?xml version="1.0" encoding="utf-8"?>
<linearlayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="@dimen/group_item_height"
 xmlns:android="http://schemas.android.com/apk/res/android">
 <textview 
 android:gravity="center" 
 android:id="@+id/groupitem" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 style="@style/grouplist_item_textview" />
</linearlayout>

好了,这些都是其中用到的布局文件,开始重头戏

package jk.activity;

import java.util.arraylist;
import java.util.list;

import android.app.activity;
import android.content.context;
import android.graphics.drawable.bitmapdrawable;
import android.os.bundle;
import android.util.log;
import android.view.layoutinflater;
import android.view.view;
import android.view.windowmanager;
import android.widget.adapterview;
import android.widget.adapterview.onitemclicklistener;
import android.widget.arrayadapter;
import android.widget.button;
import android.widget.listview;
import android.widget.popupwindow;
import android.widget.textview;
import android.widget.toast;

public class poupwindowdemoactivity extends activity {

 private popupwindow popupwindow;

 private listview lv_group;

 private view view;

 private view top_title;

 private textview tvtitle;

 private list<string> groups;
 
 private button button;
 public void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.main);
 button = (button)findviewbyid(r.id.bb);
 top_title = this.findviewbyid(r.id.top_title);

 tvtitle = (textview) top_title.findviewbyid(r.id.tvtitle);

 tvtitle.settext("天下第一");

 tvtitle.setonclicklistener(new view.onclicklistener() {

 @override
 public void onclick(view v) {
 showwindow(v);
 }
 });

 }

 /**
 * 显示
 * 
 * @param parent
 */
 private void showwindow(view parent) {

 if (popupwindow == null) {
 layoutinflater layoutinflater = (layoutinflater) getsystemservice(context.layout_inflater_service);

 view = layoutinflater.inflate(r.layout.group_list, null);

 lv_group = (listview) view.findviewbyid(r.id.lvgroup);
 // 加载数据
 groups = new arraylist<string>();
 groups.add("女儿国");
 groups.add("西游");
 groups.add("大话");
 groups.add("大唐");

 groupadapter groupadapter = new groupadapter(this, groups);
 lv_group.setadapter(groupadapter);
 // 创建一个popuwidow对象
 popupwindow = new popupwindow(view, 200, 250);
 }

 // 使其聚集
 popupwindow.setfocusable(true);
 // 设置允许在外点击消失
 popupwindow.setoutsidetouchable(true);

 // 这个是为了点击“返回back”也能使其消失,并且并不会影响你的背景
 popupwindow.setbackgrounddrawable(new bitmapdrawable());
 windowmanager windowmanager = (windowmanager) getsystemservice(context.window_service);
 // 显示的位置为:屏幕的宽度的一半-popupwindow的高度的一半
 int xpos = windowmanager.getdefaultdisplay().getwidth() / 2
 - popupwindow.getwidth() / 2;

 log.i("coder", "windowmanager.getdefaultdisplay().getwidth()/2:"
 + windowmanager.getdefaultdisplay().getwidth() / 2);
 //
 log.i("coder", "popupwindow.getwidth()/2:" + popupwindow.getwidth() / 2);

 log.i("coder", "xpos:" + xpos);

 popupwindow.showasdropdown(parent, xpos, 0);

 lv_group.setonitemclicklistener(new onitemclicklistener() {

 @override
 public void onitemclick(adapterview<?> adapterview, view view,
 int position, long id) {

 toast.maketext(poupwindowdemoactivity.this,
 "groups.get(position)" + groups.get(position), 1000)
 .show();

 if (popupwindow != null) {
 popupwindow.dismiss();
 }
 }
 });
 }
}

在这里还需要用到adapter,用到较多一般有baseadapter,simpleadapter,baseadapter挺强大的,不过simpleadapter扩展性挺不错的,可以满足自定义的需求,如果你理解输入输出流很快就能理解adapter的,他就是装备数据的

package jk.activity;

import java.util.list;



import android.content.context;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.baseadapter;
import android.widget.textview;

public class groupadapter extends baseadapter {

 private context context;

 private list<string> list;

 public groupadapter(context context, list<string> list) {

 this.context = context;
 this.list = list;

 }

 @override
 public int getcount() {
 return list.size();
 }

 @override
 public object getitem(int position) {

 return list.get(position);
 }

 @override
 public long getitemid(int position) {
 return position;
 }

 @override
 public view getview(int position, view convertview, viewgroup viewgroup) {

 
 viewholder holder;
 if (convertview==null) {
 convertview=layoutinflater.from(context).inflate(r.layout.group_item_view, null);
 holder=new viewholder();
 
 convertview.settag(holder);
 
 holder.groupitem=(textview) convertview.findviewbyid(r.id.groupitem);
 
 }
 else{
 holder=(viewholder) convertview.gettag();
 }
 holder.groupitem.settext(list.get(position));
 
 return convertview;
 }

 static class viewholder {
 textview groupitem;
 }

}

对于adapter,最重要是getview(int position, view convertview, viewgroup viewgroup)这个方法的,菜已上完,各位慢慢享用。

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