一、曾经做过一个项目,用的大概就是这种布局,title布局,title右边是ImageView,点击每一个图标,弹出PopupWin,点击其他按钮PopupWin就在原地消失。这个效果还很不错的,我挺喜欢的,所以抽空把它写了出来。现在将代码给大家提供一下,希望能给大家一些提示。先看截图:
点击MP3这个图标:点击PopupWin苹果MP3这列弹出Toast:
点击PopupWin中魅族这列弹出Toast: 点击短信这个图标:
点击新建这个图标: 点击音乐这个图标:
点击电话这个图标:点击作者这个图标:
点击PopupWin第一列弹出Toast:点击PopupWin第二列弹出Toast:
说明:每一个popupwin里面我都写了点击事件,就是弹一个toast,目前我就截图截了四张,给大家演示一下!
二:布局文件
1、activity_title.xml布局文件代码:
2、new_file.xml
三、MainActivity类,java类实现以上功能:
package com.cn.android; import android.app.Activity; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { /** Called when the activity is first created. */ DisplayMetrics metrics; int mpaddingLeft = 5; int mviewpos[] = new int[2]; int popWidth = 280; private View MenuView = null; PopupWindow pop; LinearLayout new_name; LinearLayout new_num; public static final int NEW_NAME1= 1; public static final int NEW_NUM1 = 2; public static final int NEW_NAME2= 3; public static final int NEW_NUM2 = 4; public static final int NEW_NAME3= 5; public static final int NEW_NUM3 = 6; public static final int NEW_NAME4= 7; public static final int NEW_NUM4 = 8; public static final int NEW_NAME5= 9; public static final int NEW_NUM5 = 10; public static final int NEW_NAME6= 11; public static final int NEW_NUM6 = 12; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); setContentView(R.layout.activity_title); //初始化ImageView initTitlebarBtn(); } private void initTitlebarBtn() { ImageButton title_about = (ImageButton) findViewById(R.id.title_about); title_about.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub //添加自己需要的代码 } }); LinearLayout titlephone1 = (LinearLayout) findViewById(R.id.title_mp3); titlephone1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { mpaddingLeft = 5; v.getLocationOnScreen(mviewpos); if (!(mviewpos[0] + popWidth < metrics.widthPixels)) { mpaddingLeft = mviewpos[0] - (metrics.widthPixels - popWidth); } MenuView = getLayoutInflater().inflate(R.layout.new_file, null); TextView text_row1 = (TextView)MenuView.findViewById(R.id.text1); TextView text_row2 = (TextView)MenuView.findViewById(R.id.text2); text_row1.setText("苹果MP3"); text_row2.setText("魅族MP3"); initLayout(MenuView, R.id.title_mp3); popMenu(MenuView, v); } }); LinearLayout titlephonesms = (LinearLayout) findViewById(R.id.title_phone_sms); titlephonesms.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { v.getLocationOnScreen(mviewpos); if (!(mviewpos[0] + popWidth < metrics.widthPixels)) { mpaddingLeft = mviewpos[0] - (metrics.widthPixels - popWidth); } MenuView = getLayoutInflater().inflate(R.layout.new_file, null); TextView text_row1 = (TextView)MenuView.findViewById(R.id.text1); TextView text_row2 = (TextView)MenuView.findViewById(R.id.text2); text_row1.setText("搞笑短信"); text_row2.setText("经典短信"); initLayout(MenuView, R.id.title_phone_sms); popMenu(MenuView, v); } }); LinearLayout titleaddnew = (LinearLayout) findViewById(R.id.title_addnew); titleaddnew.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { v.getLocationOnScreen(mviewpos); if (!(mviewpos[0] + popWidth < metrics.widthPixels)) { mpaddingLeft = mviewpos[0] - (metrics.widthPixels - popWidth); } MenuView = getLayoutInflater().inflate(R.layout.new_file, null); TextView text_row1 = (TextView)MenuView.findViewById(R.id.text1); TextView text_row2 = (TextView)MenuView.findViewById(R.id.text2); text_row1.setText("新建联系人"); text_row2.setText("新建记事本"); initLayout(MenuView, R.id.title_addnew); popMenu(MenuView, v); } }); LinearLayout titlemusic = (LinearLayout) findViewById(R.id.title_music); titlemusic.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { v.getLocationOnScreen(mviewpos); if (!(mviewpos[0] + popWidth < metrics.widthPixels)) { mpaddingLeft = mviewpos[0] - (metrics.widthPixels - popWidth); } MenuView = getLayoutInflater().inflate(R.layout.new_file, null); TextView text_row1 = (TextView)MenuView.findViewById(R.id.text1); TextView text_row2 = (TextView)MenuView.findViewById(R.id.text2); text_row1.setText("孙燕姿音乐"); text_row2.setText("周杰伦音乐"); initLayout(MenuView, R.id.title_music); popMenu(MenuView, v); } }); LinearLayout titlephone2 = (LinearLayout) findViewById(R.id.title_phone); titlephone2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { v.getLocationOnScreen(mviewpos); if (!(mviewpos[0] + popWidth < metrics.widthPixels)) { mpaddingLeft = mviewpos[0] - (metrics.widthPixels - popWidth); } MenuView = getLayoutInflater().inflate(R.layout.new_file, null); TextView text_row1 = (TextView)MenuView.findViewById(R.id.text1); TextView text_row2 = (TextView)MenuView.findViewById(R.id.text2); text_row1.setText("中国移动通讯"); text_row2.setText("中国联通通讯"); initLayout(MenuView, R.id.title_phone); popMenu(MenuView, v); } }); LinearLayout titleauthor = (LinearLayout) findViewById(R.id.title_author); titleauthor.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { v.getLocationOnScreen(mviewpos); if (!(mviewpos[0] + popWidth < metrics.widthPixels)) { mpaddingLeft = mviewpos[0] - (metrics.widthPixels - popWidth); } MenuView = getLayoutInflater().inflate(R.layout.new_file, null); TextView text_row1 = (TextView)MenuView.findViewById(R.id.text1); TextView text_row2 = (TextView)MenuView.findViewById(R.id.text2); text_row1.setText("信息院软件0703"); text_row2.setText("学号:070203074"); initLayout(MenuView, R.id.title_author); popMenu(MenuView, v); } }); } public void popMenu(View menuview, View view) { if (pop == null) { pop = new PopupWindow(menuview, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true); pop.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); pop.setAnimationStyle(R.style.PopupAnimation); pop.setOutsideTouchable(true); pop.showAsDropDown(view, Gravity.CENTER_HORIZONTAL, 0); pop.update(); } else { if (pop.isShowing()) { pop.dismiss(); pop = null; } else { pop = null; pop = new PopupWindow(menuview, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true); pop.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); pop.setAnimationStyle(R.style.PopupAnimation); pop.setOutsideTouchable(false); pop.showAsDropDown(view, Gravity.CENTER_HORIZONTAL, 0); pop.update(); } } } public void initLayout(final View view, int id) { LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(new LayoutParam (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); ImageView imageView = new ImageView(this); imageView.setImageResource(R.drawable.dialog_arrow2); LinearLayout aLayout; switch (id) { case R.id.title_mp3: aLayout = (LinearLayout) view.findViewById(R.id.new_file_layout); aLayout.addView(imageView, params); aLayout.setPadding(mpaddingLeft, 0, 2, 0); new_name = (LinearLayout) view.findViewById(R.id.row1); fileOnClick(NEW_NAME1, new_name); new_num = (LinearLayout) view.findViewById(R.id.row2); fileOnClick(NEW_NUM1, new_num); break; case R.id.title_phone_sms: aLayout = (LinearLayout) view.findViewById(R.id.new_file_layout); aLayout.addView(imageView, params); aLayout.setPadding(mpaddingLeft+8, 0, 0, 0); new_name = (LinearLayout) view.findViewById(R.id.row1); fileOnClick(NEW_NAME2, new_name); new_num = (LinearLayout) view.findViewById(R.id.row2); fileOnClick(NEW_NUM2, new_num); break; case R.id.title_addnew: aLayout = (LinearLayout) view.findViewById(R.id.new_file_layout); aLayout.addView(imageView, params); aLayout.setPadding(mpaddingLeft+8, 0, 0, 0); new_name = (LinearLayout) view.findViewById(R.id.row1); fileOnClick(NEW_NAME3, new_name); new_num = (LinearLayout) view.findViewById(R.id.row2); fileOnClick(NEW_NUM3, new_num); break; case R.id.title_music: aLayout = (LinearLayout) view.findViewById(R.id.new_file_layout); aLayout.addView(imageView, params); aLayout.setPadding(mpaddingLeft+8, 0, 0, 0); new_name = (LinearLayout) view.findViewById(R.id.row1); fileOnClick(NEW_NAME4, new_name); new_num = (LinearLayout) view.findViewById(R.id.row2); fileOnClick(NEW_NUM4, new_num); break; case R.id.title_phone: aLayout = (LinearLayout) view.findViewById(R.id.new_file_layout); aLayout.addView(imageView, params); aLayout.setPadding(mpaddingLeft+8, 0, 0, 0); new_name = (LinearLayout) view.findViewById(R.id.row1); fileOnClick(NEW_NAME5, new_name); new_num = (LinearLayout) view.findViewById(R.id.row2); fileOnClick(NEW_NUM5, new_num); break; case R.id.title_author: aLayout = (LinearLayout) view.findViewById(R.id.new_file_layout); aLayout.addView(imageView, params); aLayout.setPadding(mpaddingLeft+8, 0, 0, 0); new_name = (LinearLayout) view.findViewById(R.id.row1); fileOnClick(NEW_NAME6, new_name); new_num = (LinearLayout) view.findViewById(R.id.row2); fileOnClick(NEW_NUM6, new_num); break; } } public void fileOnClick(int index, final LinearLayout mLayout) { switch (index) { case NEW_NAME1: mLayout.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Toast toast_new_name1 = Toast.makeText(MainActivity.this,"This is 苹果 MP3!",Toast.LENGTH_LONG); toast_new_name1.show(); pop.dismiss(); } }); break; case NEW_NUM1: mLayout.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Toast toast_new_num1 = Toast.makeText(MainActivity.this,"This is 魅族MP3!",Toast.LENGTH_LONG); toast_new_num1.show(); pop.dismiss(); } }); break; case NEW_NAME2: mLayout.setOnClickListener(new OnClickListener() { public void onClick(View v) { / / TODO Auto-generated method stub Toast toast_new_name2 = Toast.makeText(MainActivity.this,"This is 搞笑短信!",Toast.LENGTH_LONG); toast_new_name2.show(); pop.dismiss(); } }); break; case NEW_NUM2: mLayout.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Toast toast_new_num2 = Toast.makeText(MainActivity.this,"This is 经典短信!",Toast.LENGTH_LONG); toast_new_num2.show(); pop.dismiss(); } }); break; case NEW_NAME3: mLayout.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Toast toast_new_name3 = Toast.makeText(MainActivity.this,"This is 新建联系人!",Toast.LENGTH_LONG); toast_new_name3.show(); pop.dismiss(); } }); break; case NEW_NUM3: mLayout.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Toast toast_new_num3 = Toast.makeText(MainActivity.this,"This is 新建记事本!",Toast.LENGTH_LONG); toast_new_num3.show(); pop.dismiss(); } }); break; case NEW_NAME4: mLayout.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Toast toast_new_name4 = Toast.makeText(MainActivity.this,"This is 孙燕姿音乐!",Toast.LENGTH_LONG); toast_new_name4.show(); pop.dismiss(); } }); break; case NEW_NUM4: mLayout.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Toast toast_new_num4 = Toast.makeText(MainActivity.this,"This is 周杰伦音乐!",Toast.LENGTH_LONG); toast_new_num4.show(); pop.dismiss(); } }); break; case NEW_NAME5: mLayout.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Toast toast_new_name5 = Toast.makeText(MainActivity.this,"This is 中国移动通讯!",Toast.LENGTH_LONG); toast_new_name5.show(); pop.dismiss(); } }); break; case NEW_NUM5: mLayout.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Toast toast_new_num5 = Toast.makeText(MainActivity.this,"This is 中国联通通讯!",Toast.LENGTH_LONG); toast_new_num5.show(); pop.dismiss(); } }); break; case NEW_NAME6: mLayout.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Toast toast_new_name6 = Toast.makeText(MainActivity.this,"This is 信息院软件0703!",Toast.LENGTH_LONG); toast_new_name6.show(); pop.dismiss(); } }); break; case NEW_NUM6: mLayout.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Toast toast_new_num6 = Toast.makeText(MainActivity.this,"This is 学号:070203074",Toast.LENGTH_LONG); toast_new_num6.show(); pop.dismiss(); } }); break; default: break; } } }
四、设置PopupWin,设置进入和退出的的动画:
在res目录下建anim文件夹,在里面加入布局文件:
1、popup_enter.xml:
2、ppup_exit.xml
五、在Values加入一个style.xml样式的xml文件:
六、看一下,drawable的图片,这个大家可以根据自己的要求自己在网上找图片,图片我没有上传,希望大家理解,有需要的可以留个邮箱,我可以把整个项目发给你。
1、里面有个word_view.xml: