代码下载地址 点击打开链接
自定义Popu+baseAdapter
自定义Popu+simpleAdapter
自定义Popu+xml
++++++++++++++++++++++++++++++++++++++案例一++++++++++++++++++++++++++++++++++++++++
主界面布局main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/btn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="show popWindow"/> </LinearLayout>
<?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="wrap_content" android:background="#ffffff" android:orientation="vertical" android:paddingBottom="2dp"> <View android:layout_width="match_parent" android:layout_height="2.25dp" android:background="#fa7829" android:layout_alignParentTop="true"/> <TextView android:id="@+id/pop_computer" android:layout_width="match_parent" android:layout_height="wrap_content" style="@style/pop_text_style" android:text="计算机"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@drawable/list_line"/> <TextView android:id="@+id/pop_financial" android:layout_width="match_parent" android:layout_height="wrap_content" style="@style/pop_text_style" android:text="金融"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@drawable/list_line"/> <TextView android:id="@+id/pop_manage" android:layout_width="match_parent" android:layout_height="wrap_content" style="@style/pop_text_style" android:text="管理"/> <View android:layout_width="match_parent" android:layout_height="1dp"/> </LinearLayout>
package com.harvic.Pop_showAtLocation; import android.app.Activity; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.PopupWindow; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements View.OnClickListener{ private PopupWindow mPopWindow; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showPopupWindow(); } }); } private void showPopupWindow() { //加载pop的布局文件,默认是没有布局的,需要加载布局,然后填充布局文件 View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null); /** * 1:充满屏幕 */ mPopWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); /** * 2:没有充满屏幕 */ mPopWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); //通过布局获取popup上的按钮,进行点击事件的设置 TextView tv1 = (TextView)contentView.findViewById(R.id.pop_computer); TextView tv2 = (TextView)contentView.findViewById(R.id.pop_financial); TextView tv3 = (TextView)contentView.findViewById(R.id.pop_manage); tv1.setOnClickListener(this); tv2.setOnClickListener(this); tv3.setOnClickListener(this); View rootview = LayoutInflater.from(MainActivity.this).inflate(R.layout.main, null); //如果在代码中重新设置了popupWindow的宽和高,那就以代码中所设置为准 mPopWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0); } @Override public void onClick(View v) { int id = v.getId(); switch (id){ case R.id.pop_computer:{ Toast.makeText(this, "clicked computer", Toast.LENGTH_SHORT).show(); mPopWindow.dismiss(); } break; case R.id.pop_financial:{ Toast.makeText(this,"clicked financial",Toast.LENGTH_SHORT).show(); mPopWindow.dismiss(); } break; case R.id.pop_manage:{ Toast.makeText(this,"clicked manage",Toast.LENGTH_SHORT).show(); mPopWindow.dismiss(); } break; } } }
主页面布局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="fill_parent" android:orientation="vertical" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ffffff" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:padding="10dp" android:text="返回" android:textColor="#50484b" /> <TextView android:id="@+id/menu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:padding="10dp" android:text="菜单" android:textColor="#50484b" /> </RelativeLayout> </LinearLayout>
<?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="wrap_content" android:background="@drawable/pop_bg" android:orientation="vertical" android:paddingBottom="2dp"> <TextView android:id="@+id/pop_computer" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" style="@style/pop_text_style" android:text="计算机"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@drawable/list_line"/> <TextView android:id="@+id/pop_financial" android:layout_width="fill_parent" android:gravity="center_horizontal" android:layout_height="wrap_content" style="@style/pop_text_style" android:text="金融"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@drawable/list_line"/> <TextView android:id="@+id/pop_manage" android:layout_width="fill_parent" android:gravity="center_horizontal" android:layout_height="wrap_content" style="@style/pop_text_style" android:text="管理"/> <View android:layout_width="match_parent" android:layout_height="1dp"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="pop_text_style"> <item name="android:paddingTop">10dp</item> <item name="android:paddingBottom">10dp</item> <item name="android:layout_gravity">center</item> </style> </resources>
package com.harvic.PopupShowAsDropDown; import android.app.Activity; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.PopupWindow; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements View.OnClickListener{ private PopupWindow mPopWindow; private TextView mMenuTv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mMenuTv = (TextView)findViewById(R.id.menu); mMenuTv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showPopupWindow(); } }); } private void showPopupWindow() { View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null); mPopWindow = new PopupWindow(contentView); /** * popup布局中,最好将计算机、金融、管理3个TextView设置为手势可控区域的最大区域 * 前提是: mPopWindow.setWidth(300);没有这行代码的话 * 1、如果设置LayoutParams.WRAP_CONTENT那么就不会填充满屏幕 * 2、如果设置LayoutParams.FILL_PARENT那么就会填充满屏幕 */ mPopWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); /**在这行代码上控制自己想要的高度*/ mPopWindow.setWidth(300); TextView tv1 = (TextView)contentView.findViewById(R.id.pop_computer); TextView tv2 = (TextView)contentView.findViewById(R.id.pop_financial); TextView tv3 = (TextView)contentView.findViewById(R.id.pop_manage); tv1.setOnClickListener(this); tv2.setOnClickListener(this); tv3.setOnClickListener(this); mPopWindow.setBackgroundDrawable(new BitmapDrawable()); mPopWindow.setOutsideTouchable(true); mPopWindow.showAsDropDown(mMenuTv); } @Override public void onClick(View v) { int id = v.getId(); switch (id){ case R.id.pop_computer:{ Toast.makeText(this, "clicked computer", Toast.LENGTH_SHORT).show(); mPopWindow.dismiss(); } break; case R.id.pop_financial:{ Toast.makeText(this,"clicked financial",Toast.LENGTH_SHORT).show(); mPopWindow.dismiss(); } break; case R.id.pop_manage:{ Toast.makeText(this,"clicked manage",Toast.LENGTH_SHORT).show(); mPopWindow.dismiss(); } break; } } }