PopupWin的实现

下面的转载,自我感觉很棒,对PopupWin的项目实现很有app的通用性,先推荐如下:

一、曾经做过一个项目,用的大概就是这种布局,title布局,title右边是ImageView,点击每一个图标,弹出PopupWin,点击其他按钮PopupWin就在原地消失。这个效果还很不错的,我挺喜欢的,所以抽空把它写了出来。现在将代码给大家提供一下,希望能给大家一些提示。先看截图:

                      点击MP3这个图标:                                          点击PopupWin苹果MP3这列弹出Toast:

                                                      

           

        点击PopupWin中魅族这列弹出Toast:                                点击短信这个图标:

                                             

 

                  点击新建这个图标:                                               点击音乐这个图标:

                                       

 

                   点击电话这个图标:                                                  点击作者这个图标:

                                       

                          

            点击PopupWin第一列弹出Toast:                            点击PopupWin第二列弹出Toast:

                                      

说明:每一个popupwin里面我都写了点击事件,就是弹一个toast,目前我就截图截了四张,给大家演示一下!

二:布局文件

  1、activity_title.xml布局文件代码:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="@drawable/title_bg_button">  
  6.        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  7.                 android:orientation="horizontal" android:layout_width="fill_parent"  
  8.                 android:layout_height="78dip" android:background="@drawable/title_bg">  
  9.                 <RelativeLayout android:id="@+id/RelativeLayout_title"  
  10.                          android:layout_height="wrap_content" android:layout_width="wrap_content"  
  11.                          android:layout_gravity="center_horizontal|center_vertical"  
  12.                          android:layout_weight="1.0">  
  13.                          <ImageButton android:id="@+id/title_about"  
  14.                                     android:layout_width="wrap_content" android:layout_height="wrap_content"  
  15.                                     android:background="@drawable/main_top_image" />  
  16.                </RelativeLayout>  
  17.        <LinearLayout android:id="@+id/LinearLayout_title02"  
  18.                  android:gravity="right" android:layout_gravity="right|center"  
  19.                  android:layout_marginRight="10sp" android:layout_width="wrap_content"  
  20.                  android:layout_height="wrap_content" android:orientation="horizontal"  
  21.                  android:layout_marginTop="10dip">  
  22.                   <LinearLayout android:id="@+id/title_mp3"  
  23.                            android:paddingRight="5px"  
  24.                            android:layout_width="wrap_content" android:layout_height="wrap_content"  
  25.                            android:orientation="vertical" android:gravity="center"  
  26.                             >  
  27.                             <ImageView android:id="@+id/gridlist_image"  
  28.                                      android:background="@drawable/title1" android:layout_width="32dip"  
  29.                                      android:scaleType="centerInside"  
  30.                                      android:layout_height="32dip"></ImageView>  
  31.                             <TextView android:id="@+id/grid_list_text"  
  32.                                       android:layout_width="wrap_content" android:layout_height="wrap_content"  
  33.                                       android:textColor="#ffffffff" android:text="MP3"  
  34.                                       android:textSize="13dp" />  
  35.                    </LinearLayout>  
  36.                    <LinearLayout android:id="@+id/title_phone_sms"  
  37.                              android:paddingLeft="5px" android:paddingRight="5px"  
  38.                              android:layout_width="wrap_content" android:layout_height="wrap_content"  
  39.                              android:orientation="vertical" android:gravity="center"  
  40.                       >  
  41.                             <ImageView android:id="@+id/title_new_doc"  
  42.                                       android:layout_width="32dip" android:layout_height="32dip"  
  43.                                       android:scaleType="centerInside"  
  44.                                       android:background="@drawable/title2">  
  45.                            </ImageView>  
  46.                            <TextView android:layout_width="wrap_content"  
  47.                                       android:layout_height="wrap_content" android:textColor="#ffffffff"  
  48.                                       android:text="短信" android:textSize="13dp" />  
  49.                    </LinearLayout>  
  50.                    <LinearLayout android:paddingLeft="5px" android:paddingRight="5px"  
  51.                            android:layout_width="wrap_content" android:layout_height="wrap_content"  
  52.                            android:orientation="vertical" android:gravity="center"  
  53.                            android:id="@+id/title_addnew">  
  54.                            <ImageView android:id="@+id/imageView1" android:background="@drawable/title3"  
  55.                                     android:layout_width="32dip" android:layout_height="32dip"  
  56.                                     android:scaleType="centerInside"/>  
  57.                            <TextView android:textColor="#ffffffff" android:id="@+id/textView1"  
  58.                                      android:textSize="13dp" android:text="新建"  
  59.                                      android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>  
  60.                   </LinearLayout>  
  61.                   <LinearLayout android:id="@+id/title_music"  
  62.                            android:paddingLeft="5px" android:paddingRight="5px"  
  63.                            android:layout_width="wrap_content" android:layout_height="wrap_content"  
  64.                            android:orientation="vertical" android:gravity="center"  
  65.                     >  
  66.                            <ImageView android:layout_width="32dip"  
  67.                                     android:layout_height="32dip" android:background="@drawable/title4"  
  68.                                     android:scaleType="centerInside"></ImageView>  
  69.                             <TextView android:layout_width="wrap_content"  
  70.                                     android:layout_height="wrap_content" android:textColor="#ffffffff"  
  71.                                     android:text="音乐" android:textSize="13dp" />  
  72.                  </LinearLayout>  
  73.                 <LinearLayout android:id="@+id/title_phone"  
  74.                           android:paddingLeft="5px" android:paddingRight="5px"  
  75.                           android:layout_width="wrap_content" android:layout_height="wrap_content"  
  76.                           android:orientation="vertical" android:gravity="center"  
  77.                   >  
  78.                           <ImageView android:layout_width="32dip"  
  79.                                    android:layout_height="32dip" android:background="@drawable/title5"  
  80.                                    android:scaleType="centerInside"></ImageView>  
  81.                           <TextView android:layout_width="wrap_content"  
  82.                                    android:layout_height="wrap_content" android:textColor="#ffffffff"  
  83.                                    android:text="电话"  
  84.                                    android:textSize="13dp" />  
  85.                 </LinearLayout>  
  86.                 <LinearLayout android:id="@+id/title_author"  
  87.                          android:paddingLeft="5px"  
  88.                          android:layout_width="wrap_content" android:layout_height="wrap_content"  
  89.                          android:orientation="vertical" android:gravity="center"  
  90.                  >  
  91.                          <ImageView android:id="@+id/title_back_image"  
  92.                                  android:background="@drawable/title6"  
  93.                                  android:layout_width="32dip" android:layout_height="32dip"  
  94.                                  android:scaleType="centerInside">  
  95.                          </ImageView>  
  96.                          <TextView android:id="@+id/title_back_text"  
  97.                                   android:layout_width="wrap_content" android:layout_height="wrap_content"  
  98.                                   android:textColor="#ffffff" android:text="作者"  
  99.                                   android:textSize="13dp" />  
  100.                 </LinearLayout>  
  101.           </LinearLayout>  
  102.     </LinearLayout>  
  103. </LinearLayout>  

 2、new_file.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.  android:orientation="vertical" android:layout_width="280px"  
  4.  android:layout_height="wrap_content">  
  5.    
  6.      <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical">  
  7.              <LinearLayout android:layout_width="wrap_content" android:layout_height="19dp"></LinearLayout>  
  8.              <LinearLayout android:id="@+id/new_file_text" android:layout_width="280px"  
  9.                        android:layout_height="wrap_content" android:background="@drawable/dialog_title1">  
  10.                        <TextView android:layout_width="fill_parent"  
  11.                                  android:layout_height="wrap_content" android:layout_gravity="center_vertical"  
  12.                                  android:gravity="center_horizontal" android:text="大明原创" />  
  13.             </LinearLayout>  
  14.      </LinearLayout>  
  15.      <LinearLayout android:layout_width="fill_parent"  
  16.              android:layout_height="19dp"   
  17.              android:id="@+id/new_file_layout"  
  18.              android:paddingLeft="8dip"  
  19.              android:layout_marginTop="-48dp">  
  20.       </LinearLayout>  
  21.       <LinearLayout android:id="@+id/LinearLayout_Viewer1" android:layout_marginTop="28dp"  
  22.              android:layout_width="280px" android:layout_height="wrap_content"  
  23.              android:orientation="vertical"  
  24.              android:paddingLeft="10px"  
  25.              android:paddingRight="10px"  
  26.              android:background="@drawable/dialog_body1"  
  27.              android:layout_weight="1.0">  
  28.             <LinearLayout android:layout_width="fill_parent"  
  29.                      android:layout_height="30dip" android:background="@drawable/word_view"  
  30.                      android:paddingLeft="10dip" android:id="@+id/row1">  
  31.                     <RelativeLayout android:layout_width="wrap_content"  
  32.                              android:layout_height="wrap_content" android:layout_gravity="center_vertical|center_horizontal">  
  33.                              <TextView android:layout_width="wrap_content"  
  34.                                        android:layout_height="wrap_content"  
  35.                                        android:layout_centerHorizontal="true"  
  36.                                        android:layout_centerVertical="true"  
  37.                                        android:layout_marginLeft="60dip"  
  38.                                        style="@style/TextFormat" android:id="@+id/text1" />  
  39.                    </RelativeLayout>  
  40.             </LinearLayout>  
  41.             <LinearLayout android:layout_width="fill_parent"  
  42.                     android:layout_height="2dip" android:background="#ff888888" />  
  43.                     <LinearLayout android:layout_width="fill_parent"  
  44.                              android:layout_height="30dip" android:background="@drawable/word_view"  
  45.                              android:paddingLeft="10dip" android:id="@+id/row2">  
  46.                             <RelativeLayout android:layout_width="wrap_content"  
  47.                                      android:layout_height="wrap_content" android:layout_gravity="center_vertical|center_horizontal">  
  48.                                     <TextView android:layout_width="wrap_content"  
  49.                                               android:layout_height="wrap_content"  
  50.                                               android:layout_centerHorizontal="true"  
  51.                                               android:layout_centerVertical="true"  
  52.                                               android:layout_marginLeft="60dip"  
  53.                                               style="@style/TextFormat" android:id="@+id/text2" />  
  54.                            </RelativeLayout>  
  55.                    </LinearLayout>  
  56.            </LinearLayout>  
  57.            <LinearLayout android:layout_width="280px"  
  58.                     android:layout_height="wrap_content" android:background="@drawable/dialog_bottom1" />  
  59. </LinearLayout>  

三、MainActivity类,java类实现以上功能:

  1. package com.cn.android;  
  2. import android.app.Activity;  
  3. import android.graphics.Color;  
  4. import android.graphics.drawable.ColorDrawable;  
  5. import android.os.Bundle;  
  6. import android.util.DisplayMetrics;  
  7. import android.view.Gravity;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.view.ViewGroup.LayoutParams;  
  11. import android.widget.ImageButton;  
  12. import android.widget.ImageView;  
  13. import android.widget.LinearLayout;  
  14. import android.widget.PopupWindow;  
  15. import android.widget.TextView;  
  16. import android.widget.Toast;  
  17. public class MainActivity extends Activity {  
  18.     /** Called when the activity is first created. */  
  19.    
  20.           DisplayMetrics metrics;  
  21.           int mpaddingLeft = 5;  
  22.           int mviewpos[] = new int[2];  
  23.           int popWidth = 280;  
  24.           private View MenuView = null;  
  25.           PopupWindow pop;  
  26.           LinearLayout new_name;  
  27.           LinearLayout new_num;  
  28.           public static final int NEW_NAME1= 1;  
  29.           public static final int NEW_NUM1 = 2;  
  30.           public static final int NEW_NAME2= 3;  
  31.           public static final int NEW_NUM2 = 4;  
  32.           public static final int NEW_NAME3= 5;  
  33.           public static final int NEW_NUM3 = 6;  
  34.           public static final int NEW_NAME4= 7;  
  35.           public static final int NEW_NUM4 = 8;  
  36.           public static final int NEW_NAME5= 9;  
  37.           public static final int NEW_NUM5 = 10;  
  38.           public static final int NEW_NAME6= 11;  
  39.           public static final int NEW_NUM6 = 12;  
  40.    
  41.    
  42.          @Override  
  43.          public void onCreate(Bundle savedInstanceState) {  
  44.                  super.onCreate(savedInstanceState);  
  45.   
  46.                  metrics = new DisplayMetrics();  
  47.                  getWindowManager().getDefaultDisplay().getMetrics(metrics);  
  48.                  setContentView(R.layout.activity_title);  
  49.                  //初始化ImageView  
  50.                  initTitlebarBtn();  
  51.           }   
  52.       
  53.           private void initTitlebarBtn() {    
  54.                  ImageButton title_about = (ImageButton) findViewById(R.id.title_about);  
  55.                  title_about.setOnClickListener(new View.OnClickListener() {  
  56.                          public void onClick(View v) {  
  57.                                  // TODO Auto-generated method stub  
  58.                                  //添加自己需要的代码  
  59.                            }  
  60.                  });  
  61.     
  62.                  LinearLayout titlephone1 = (LinearLayout) findViewById(R.id.title_mp3);  
  63.                  titlephone1.setOnClickListener(new View.OnClickListener() {  
  64.      
  65.                           public void onClick(View v) {  
  66.                                     mpaddingLeft = 5;  
  67.                                     v.getLocationOnScreen(mviewpos);  
  68.                                     if (!(mviewpos[0] + popWidth < metrics.widthPixels)) {  
  69.                                            mpaddingLeft = mviewpos[0] - (metrics.widthPixels - popWidth);  
  70.                                      }   
  71.                                     MenuView = getLayoutInflater().inflate(R.layout.new_file, null);  
  72.                                     TextView text_row1 = (TextView)MenuView.findViewById(R.id.text1);  
  73.                                     TextView text_row2 = (TextView)MenuView.findViewById(R.id.text2);  
  74.                                     text_row1.setText("苹果MP3");  
  75.                                     text_row2.setText("魅族MP3");  
  76.                                     initLayout(MenuView, R.id.title_mp3);  
  77.                                     popMenu(MenuView, v);    
  78.                            }  
  79.                  });  
  80.   
  81.                  LinearLayout titlephonesms = (LinearLayout) findViewById(R.id.title_phone_sms);  
  82.                  titlephonesms.setOnClickListener(new View.OnClickListener() {  
  83.   
  84.                              public void onClick(View v) {  
  85.                                       v.getLocationOnScreen(mviewpos);  
  86.                                       if (!(mviewpos[0] + popWidth < metrics.widthPixels)) {  
  87.                                                 mpaddingLeft = mviewpos[0] - (metrics.widthPixels - popWidth);  
  88.                                       }  
  89.                                       MenuView = getLayoutInflater().inflate(R.layout.new_file, null);  
  90.                                       TextView text_row1 = (TextView)MenuView.findViewById(R.id.text1);  
  91.                                       TextView text_row2 = (TextView)MenuView.findViewById(R.id.text2);  
  92.                                        text_row1.setText("搞笑短信");  
  93.                                        text_row2.setText("经典短信");  
  94.                                        initLayout(MenuView, R.id.title_phone_sms);  
  95.                                        popMenu(MenuView, v);  
  96.                                }  
  97.                     });  
  98.    
  99.                     LinearLayout titleaddnew = (LinearLayout) findViewById(R.id.title_addnew);  
  100.                     titleaddnew.setOnClickListener(new View.OnClickListener() {   
  101.   
  102.                                 public void onClick(View v) {  
  103.                                          v.getLocationOnScreen(mviewpos);  
  104.                                          if (!(mviewpos[0] + popWidth < metrics.widthPixels)) {  
  105.                                                   mpaddingLeft = mviewpos[0] - (metrics.widthPixels - popWidth);  
  106.                                          }  
  107.                                           MenuView = getLayoutInflater().inflate(R.layout.new_file, null);  
  108.                                           TextView text_row1 = (TextView)MenuView.findViewById(R.id.text1);  
  109.                                           TextView text_row2 = (TextView)MenuView.findViewById(R.id.text2);  
  110.                                            text_row1.setText("新建联系人");  
  111.                                            text_row2.setText("新建记事本");  
  112.                                            initLayout(MenuView, R.id.title_addnew);  
  113.                                            popMenu(MenuView, v);  
  114.                                  }  
  115.                       });  
  116.     
  117.                      LinearLayout titlemusic = (LinearLayout) findViewById(R.id.title_music);  
  118.                      titlemusic.setOnClickListener(new View.OnClickListener() {  
  119.   
  120.                                  public void onClick(View v) {  
  121.                                          v.getLocationOnScreen(mviewpos);  
  122.                                          if (!(mviewpos[0] + popWidth < metrics.widthPixels)) {  
  123.                                                   mpaddingLeft = mviewpos[0] - (metrics.widthPixels - popWidth);  
  124.                                          }  
  125.                                          MenuView = getLayoutInflater().inflate(R.layout.new_file, null);  
  126.                                          TextView text_row1 = (TextView)MenuView.findViewById(R.id.text1);  
  127.                                          TextView text_row2 = (TextView)MenuView.findViewById(R.id.text2);  
  128.                                           text_row1.setText("孙燕姿音乐");  
  129.                                           text_row2.setText("周杰伦音乐");  
  130.                                           initLayout(MenuView, R.id.title_music);  
  131.                                           popMenu(MenuView, v);  
  132.                                  }  
  133.                        });  
  134.    
  135.                        LinearLayout titlephone2 = (LinearLayout) findViewById(R.id.title_phone);  
  136.                        titlephone2.setOnClickListener(new View.OnClickListener() {  
  137.   
  138.                                 public void onClick(View v) {  
  139.                                        v.getLocationOnScreen(mviewpos);  
  140.                                        if (!(mviewpos[0] + popWidth < metrics.widthPixels)) {  
  141.                                               mpaddingLeft = mviewpos[0] - (metrics.widthPixels - popWidth);  
  142.                                         }  
  143.                                        MenuView = getLayoutInflater().inflate(R.layout.new_file, null);  
  144.                                        TextView text_row1 = (TextView)MenuView.findViewById(R.id.text1);  
  145.                                        TextView text_row2 = (TextView)MenuView.findViewById(R.id.text2);  
  146.                                        text_row1.setText("中国移动通讯");  
  147.                                        text_row2.setText("中国联通通讯");  
  148.                                        initLayout(MenuView, R.id.title_phone);  
  149.                                        popMenu(MenuView, v);  
  150.                                }  
  151.                        });  
  152.   
  153.    
  154.                       LinearLayout titleauthor = (LinearLayout) findViewById(R.id.title_author);  
  155.                       titleauthor.setOnClickListener(new View.OnClickListener() {  
  156.      
  157.                                public void onClick(View v) {  
  158.                                         v.getLocationOnScreen(mviewpos);  
  159.                                         if (!(mviewpos[0] + popWidth < metrics.widthPixels)) {  
  160.                                                  mpaddingLeft = mviewpos[0] - (metrics.widthPixels - popWidth);  
  161.                                          }  
  162.                                          MenuView = getLayoutInflater().inflate(R.layout.new_file, null);  
  163.                                          TextView text_row1 = (TextView)MenuView.findViewById(R.id.text1);  
  164.                                          TextView text_row2 = (TextView)MenuView.findViewById(R.id.text2);  
  165.                                           text_row1.setText("信息院软件0703");  
  166.                                           text_row2.setText("学号:070203074");  
  167.                                           initLayout(MenuView, R.id.title_author);  
  168.                                           popMenu(MenuView, v);  
  169.                                }  
  170.                        });  
  171.            }  
  172.       
  173.            public void popMenu(View menuview, View view) {  
  174.   
  175.                        if (pop == null) {  
  176.                                 pop = new PopupWindow(menuview, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);  
  177.                                 pop.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));  
  178.                                 pop.setAnimationStyle(R.style.PopupAnimation);  
  179.                                 pop.setOutsideTouchable(true);  
  180.                                 pop.showAsDropDown(view, Gravity.CENTER_HORIZONTAL, 0);  
  181.                                 pop.update();  
  182.                       } else {  
  183.                       if (pop.isShowing()) {  
  184.                                 pop.dismiss();  
  185.                                 pop = null;  
  186.                        } else {  
  187.                                 pop = null;  
  188.                                 pop = new PopupWindow(menuview, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);  
  189.                                 pop.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));  
  190.                                 pop.setAnimationStyle(R.style.PopupAnimation);  
  191.                                 pop.setOutsideTouchable(false);  
  192.                                 pop.showAsDropDown(view, Gravity.CENTER_HORIZONTAL, 0);  
  193.                                 pop.update();  
  194.                         }  
  195.                }  
  196.         }  
  197.       
  198.           public void initLayout(final View view, int id) {  
  199.                     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(new   LayoutParam (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
  200.                     ImageView imageView = new ImageView(this);  
  201.                     imageView.setImageResource(R.drawable.dialog_arrow2);  
  202.                     LinearLayout aLayout;  
  203.                     switch (id) {  
  204.                              case R.id.title_mp3:  
  205.                                           aLayout = (LinearLayout) view.findViewById(R.id.new_file_layout);  
  206.                                           aLayout.addView(imageView, params);  
  207.                                           aLayout.setPadding(mpaddingLeft, 020);  
  208.                                           new_name = (LinearLayout) view.findViewById(R.id.row1);  
  209.                                           fileOnClick(NEW_NAME1, new_name);  
  210.                                           new_num = (LinearLayout) view.findViewById(R.id.row2);  
  211.                                           fileOnClick(NEW_NUM1, new_num);  
  212.                                           break;  
  213.                              case R.id.title_phone_sms:  
  214.                                           aLayout = (LinearLayout) view.findViewById(R.id.new_file_layout);  
  215.                                           aLayout.addView(imageView, params);  
  216.                                           aLayout.setPadding(mpaddingLeft+8000);  
  217.                                           new_name = (LinearLayout) view.findViewById(R.id.row1);  
  218.                                           fileOnClick(NEW_NAME2, new_name);  
  219.                                           new_num = (LinearLayout) view.findViewById(R.id.row2);  
  220.                                           fileOnClick(NEW_NUM2, new_num);  
  221.                                           break;  
  222.                              case R.id.title_addnew:  
  223.                                           aLayout = (LinearLayout) view.findViewById(R.id.new_file_layout);  
  224.                                           aLayout.addView(imageView, params);  
  225.                                           aLayout.setPadding(mpaddingLeft+8000);  
  226.                                           new_name = (LinearLayout) view.findViewById(R.id.row1);  
  227.                                           fileOnClick(NEW_NAME3, new_name);  
  228.                                           new_num = (LinearLayout) view.findViewById(R.id.row2);  
  229.                                           fileOnClick(NEW_NUM3, new_num);  
  230.                                           break;  
  231.                              case R.id.title_music:  
  232.                                           aLayout = (LinearLayout) view.findViewById(R.id.new_file_layout);  
  233.                                           aLayout.addView(imageView, params);  
  234.                                           aLayout.setPadding(mpaddingLeft+8000);  
  235.                                           new_name = (LinearLayout) view.findViewById(R.id.row1);  
  236.                                           fileOnClick(NEW_NAME4, new_name);  
  237.                                           new_num = (LinearLayout) view.findViewById(R.id.row2);  
  238.                                           fileOnClick(NEW_NUM4, new_num);  
  239.                                           break;  
  240.                              case R.id.title_phone:  
  241.                                           aLayout = (LinearLayout) view.findViewById(R.id.new_file_layout);  
  242.                                           aLayout.addView(imageView, params);  
  243.                                           aLayout.setPadding(mpaddingLeft+8000);  
  244.                                           new_name = (LinearLayout) view.findViewById(R.id.row1);  
  245.                                           fileOnClick(NEW_NAME5, new_name);  
  246.                                           new_num = (LinearLayout) view.findViewById(R.id.row2);  
  247.                                           fileOnClick(NEW_NUM5, new_num);  
  248.                                           break;  
  249.                              case R.id.title_author:  
  250.                                           aLayout = (LinearLayout) view.findViewById(R.id.new_file_layout);  
  251.                                           aLayout.addView(imageView, params);  
  252.                                           aLayout.setPadding(mpaddingLeft+8000);  
  253.                                           new_name = (LinearLayout) view.findViewById(R.id.row1);  
  254.                                           fileOnClick(NEW_NAME6, new_name);  
  255.                                           new_num = (LinearLayout) view.findViewById(R.id.row2);  
  256.                                           fileOnClick(NEW_NUM6, new_num);  
  257.                                           break;  
  258.                            }  
  259.               }  
  260.                 
  261.               public void fileOnClick(int index, final LinearLayout mLayout) {  
  262.                           switch (index) {  
  263.                                       case NEW_NAME1:  
  264.                                                mLayout.setOnClickListener(new OnClickListener() {     
  265.                                                public void onClick(View v) {  
  266.                                                         // TODO Auto-generated method stub  
  267.                                                         Toast toast_new_name1 = Toast.makeText(MainActivity.this,"This is 苹果           MP3!",Toast.LENGTH_LONG);  
  268.                                                          toast_new_name1.show();  
  269.                                                          pop.dismiss();  
  270.                                                          }  
  271.                                                 });  
  272.                                         break;  
  273.                                         case NEW_NUM1:  
  274.                                                   mLayout.setOnClickListener(new OnClickListener() {  
  275.                                                   public void onClick(View v) {  
  276.                                                               // TODO Auto-generated method stub  
  277.                                                              Toast toast_new_num1 = Toast.makeText(MainActivity.this,"This is 魅族MP3!",Toast.LENGTH_LONG);  
  278.                                                               toast_new_num1.show();  
  279.                                                               pop.dismiss();  
  280.                                                               }  
  281.                                                    });  
  282.                                           break;  
  283.                                           case NEW_NAME2:  
  284.                                                    mLayout.setOnClickListener(new OnClickListener() {     
  285.                                                    public void onClick(View v) {  
  286.                                                               / / TODO Auto-generated method stub  
  287.                                                               Toast toast_new_name2 = Toast.makeText(MainActivity.this,"This is 搞笑短信!",Toast.LENGTH_LONG);  
  288.                                                                toast_new_name2.show();  
  289.                                                                pop.dismiss();  
  290.                                                                }  
  291.                                                      });  
  292.                                            break;  
  293.                                            case NEW_NUM2:  
  294.                                                     mLayout.setOnClickListener(new OnClickListener() {  
  295.                                                     public void onClick(View v) {  
  296.                                                                // TODO Auto-generated method stub  
  297.                                                                Toast toast_new_num2 = Toast.makeText(MainActivity.this,"This is 经典短信!",Toast.LENGTH_LONG);  
  298.                                                                toast_new_num2.show();  
  299.                                                                pop.dismiss();  
  300.                                                                }  
  301.                                                    });  
  302.                                              break;  
  303.                                              case NEW_NAME3:  
  304.                                                       mLayout.setOnClickListener(new OnClickListener() {     
  305.                                                       public void onClick(View v) {  
  306.                                                                 // TODO Auto-generated method stub  
  307.                                                                Toast toast_new_name3 = Toast.makeText(MainActivity.this,"This is 新建联系人!",Toast.LENGTH_LONG);  
  308.                                                                 toast_new_name3.show();  
  309.                                                                 pop.dismiss();  
  310.                                                                  }  
  311.                                                          });  
  312.                                                 break;  
  313.                                                 case NEW_NUM3:  
  314.                                                           mLayout.setOnClickListener(new OnClickListener() {  
  315.                                                           public void onClick(View v) {  
  316.                                                                      // TODO Auto-generated method stub  
  317.                                                                       Toast toast_new_num3 = Toast.makeText(MainActivity.this,"This is 新建记事本!",Toast.LENGTH_LONG);  
  318.                                                                        toast_new_num3.show();  
  319.                                                                        pop.dismiss();  
  320.                                                                        }  
  321.                                                              });  
  322.                                                    break;  
  323.                                                    case NEW_NAME4:  
  324.                                                             mLayout.setOnClickListener(new OnClickListener() {     
  325.                                                             public void onClick(View v) {  
  326.                                                                        // TODO Auto-generated method stub  
  327.                                                                        Toast toast_new_name4 = Toast.makeText(MainActivity.this,"This is 孙燕姿音乐!",Toast.LENGTH_LONG);  
  328.                                                                        toast_new_name4.show();  
  329.                                                                        pop.dismiss();  
  330.                                                                         }  
  331.                                                                });  
  332.                                                        break;  
  333.                                                        case NEW_NUM4:  
  334.                                                                  mLayout.setOnClickListener(new OnClickListener() {  
  335.                                                                  public void onClick(View v) {  
  336.                                                                  // TODO Auto-generated method stub  
  337.                                                                  Toast toast_new_num4 = Toast.makeText(MainActivity.this,"This is 周杰伦音乐!",Toast.LENGTH_LONG);  
  338.                                                                  toast_new_num4.show();  
  339.                                                                  pop.dismiss();  
  340.                                                                  }  
  341.                                                           });  
  342.                                                   break;  
  343.                                                   case NEW_NAME5:  
  344.                                                             mLayout.setOnClickListener(new OnClickListener() {     
  345.                                                             public void onClick(View v) {  
  346.                                                                        // TODO Auto-generated method stub  
  347.                                                                        Toast toast_new_name5 = Toast.makeText(MainActivity.this,"This is 中国移动通讯!",Toast.LENGTH_LONG);  
  348.                                                                        toast_new_name5.show();  
  349.                                                                        pop.dismiss();  
  350.                                                                         }  
  351.                                                                 });  
  352.                                                        break;  
  353.                                                        case NEW_NUM5:  
  354.                                                                  mLayout.setOnClickListener(new OnClickListener() {  
  355.                                                                  public void onClick(View v) {  
  356.                                                                              // TODO Auto-generated method stub  
  357.                                                                               Toast toast_new_num5 = Toast.makeText(MainActivity.this,"This is 中国联通通讯!",Toast.LENGTH_LONG);  
  358.                                                                                toast_new_num5.show();  
  359.                                                                                pop.dismiss();  
  360.                                                                                }  
  361.                                                                       });  
  362.                                                              break;  
  363.                                                              case NEW_NAME6:  
  364.                                                                        mLayout.setOnClickListener(new OnClickListener() {     
  365.                                                                        public void onClick(View v) {  
  366.                                                                                   // TODO Auto-generated method stub  
  367.                                                                                   Toast toast_new_name6 = Toast.makeText(MainActivity.this,"This is 信息院软件0703!",Toast.LENGTH_LONG);  
  368.                                                                                    toast_new_name6.show();  
  369.                                                                                    pop.dismiss();  
  370.                                                                                    }  
  371.                                                                           });  
  372.                                                                 break;  
  373.                                                                 case NEW_NUM6:  
  374.                                                                           mLayout.setOnClickListener(new OnClickListener() {  
  375.                                                                           public void onClick(View v) {  
  376.                                                                                       // TODO Auto-generated method stub  
  377.                                                                                       Toast toast_new_num6 = Toast.makeText(MainActivity.this,"This is 学号:070203074",Toast.LENGTH_LONG);  
  378.                                                                                       toast_new_num6.show();  
  379.                                                                                       pop.dismiss();  
  380.                                                                                       }  
  381.                                                                               });  
  382.                                                                    break;  
  383.                                                                    default:  
  384.                                                                     break;  
  385.                                               }  
  386.                          }  
  387. }  

四、设置PopupWin,设置进入和退出的的动画:

在res目录下建anim文件夹,在里面加入布局文件:

1、popup_enter.xml:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <translate android:duration="800" android:fromYDelta="-100%"  
  4.      android:toYDelta="0.0" />  
  5. <!--在原地弹出效果--><set  
  6.       android:fromXScale="0.0" android:toXScale="1.0"  
  7.       android:fromYScale="0.0" android:toYScale="1.0" android:pivotX="50%"  
  8.       android:pivotY="50%" android:duration="1000" />  
  9.        <alpha android:interpolator="@android:anim/decelerate_interpolator"  
  10.         android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="1000" />  
  11. -->  
  12. </set>  

2、ppup_exit.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <scale android:fromXScale="1.0"   
  4.           android:toXScale="0.5"  
  5.           android:fromYScale="1.0"   
  6.           android:toYScale="0.5"  
  7.           android:pivotX="50%"   
  8.           android:pivotY="50%"   
  9.           android:duration="500"/>  
  10.     <alpha android:interpolator="@android:anim/decelerate_interpolator"  
  11.           android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="500"/>  
  12. </set>  

五、在Values加入一个style.xml样式的xml文件:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.  <style name="TextFormat">  
  4.           <item name="android:textSize">15dip</item>  
  5.           <item name="android:textColor">#ff000000</item>  
  6.  </style>  
  7.    
  8.  <style name="PopupAnimation" parent="android:Animation" mce_bogus="1">  
  9.             <item name="android:windowEnterAnimation">@anim/popup_enter</item>  
  10.             <item name="android:windowExitAnimation">@anim/popup_exit</item>  
  11.  </style>  
  12. </resources>  

六、看一下,drawable的图片,这个大家可以根据自己的要求自己在网上找图片,图片我没有上传,希望大家理解,有需要的可以留个邮箱,我可以把整个项目发给你。

 

1、里面有个word_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="
http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/button_middle_sel" />
    <item android:state_selected ="true" android:drawable="@drawable/button_middle_sel" />
</selector>

出处:http://blog.csdn.net/wdaming1986/article/details/6636112
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值