2025-05-17

一、曾經做過一個項目,用的大概就是這種佈局,title佈局,title右邊是ImageView,點擊每一個圖標,彈出PopupWin,點擊其他按鈕PopupWin就在原地消失。這個效果還很不錯的,我挺喜歡的,所以抽空把它寫瞭出來。現在將代碼給大傢提供一下,希望能給大傢一些提示。先看截圖:

 

                      點擊MP3這個圖標:                                          點擊PopupWin蘋果MP3這列彈出Toast:

                                                      

           

        點擊PopupWin中魅族這列彈出Toast:                                點擊短信這個圖標:

                                             

 

                  點擊新建這個圖標:                                               點擊音樂這個圖標:

                                       

 

                   點擊電話這個圖標:                                                  點擊作者這個圖標:

                                       

                          

            點擊PopupWin第一列彈出Toast:                            點擊PopupWin第二列彈出Toast:

                                      

說明:每一個popupwin裡面我都寫瞭點擊事件,就是彈一個toast,目前我就截圖截瞭四張,給大傢演示一下!

 

 

二:佈局文件

  1、activity_title.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:background="@drawable/title_bg_button"> 

       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

                android:orientation="horizontal" android:layout_width="fill_parent" 

                android:layout_height="78dip" android:background="@drawable/title_bg"> 

                <RelativeLayout android:id="@+id/RelativeLayout_title" 

                         android:layout_height="wrap_content" android:layout_width="wrap_content" 

                         android:layout_gravity="center_horizontal|center_vertical" 

                         android:layout_weight="1.0"> 

                         <ImageButton android:id="@+id/title_about" 

                                    android:layout_width="wrap_content" android:layout_height="wrap_content" 

                                    android:background="@drawable/main_top_image" /> 

               </RelativeLayout> 

       <LinearLayout android:id="@+id/LinearLayout_title02" 

                 android:gravity="right" android:layout_gravity="right|center" 

                 android:layout_marginRight="10sp" android:layout_width="wrap_content" 

                 android:layout_height="wrap_content" android:orientation="horizontal" 

                 android:layout_marginTop="10dip"> 

                  <LinearLayout android:id="@+id/title_mp3" 

                           android:paddingRight="5px" 

                           android:layout_width="wrap_content" android:layout_height="wrap_content" 

                           android:orientation="vertical" android:gravity="center" 

                            > 

                            <ImageView android:id="@+id/gridlist_image" 

                                     android:background="@drawable/title1" android:layout_width="32dip" 

                                     android:scaleType="centerInside" 

                                     android:layout_height="32dip"></ImageView> 

                            <TextView android:id="@+id/grid_list_text" 

                                      android:layout_width="wrap_content" android:layout_height="wrap_content" 

                                      android:textColor="#ffffffff" android:text="MP3" 

                                      android:textSize="13dp" /> 

                   </LinearLayout> 

                   <LinearLayout android:id="@+id/title_phone_sms" 

                             android:paddingLeft="5px" android:paddingRight="5px" 

                             android:layout_width="wrap_content" android:layout_height="wrap_content" 

                             android:orientation="vertical" android:gravity="center" 

                      > 

                            <ImageView android:id="@+id/title_new_doc" 

                                      android:layout_width="32dip" android:layout_height="32dip" 

                                      android:scaleType="centerInside" 

                                      android:background="@drawable/title2"> 

                           </ImageView> 

                           <TextView android:layout_width="wrap_content" 

                                      android:layout_height="wrap_content" android:textColor="#ffffffff" 

                                      android:text="短信" android:textSize="13dp" /> 

                   </LinearLayout> 

                   <LinearLayout android:paddingLeft="5px" android:paddingRight="5px" 

                           android:layout_width="wrap_content" android:layout_height="wrap_content" 

                           android:orientation="vertical" android:gravity="center" 

                           android:id="@+id/title_addnew"> 

                           <ImageView android:id="@+id/imageView1" android:background="@drawable/title3" 

                                    android:layout_width="32dip" android:layout_height="32dip" 

                                    android:scaleType="centerInside"/> 

                           <TextView android:textColor="#ffffffff" android:id="@+id/textView1" 

                                     android:textSize="13dp" android:text="新建" 

                                     android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> 

                  </LinearLayout> 

                  <LinearLayout android:id="@+id/title_music" 

                           android:paddingLeft="5px" android:paddingRight="5px" 

                           android:layout_width="wrap_content" android:layout_height="wrap_content" 

                           android:orientation="vertical" android:gravity="center" 

                    > 

                           <ImageView android:layout_width="32dip" 

                                    android:layout_height="32dip" android:background="@drawable/title4" 

                                    android:scaleType="centerInside"></ImageView> 

                            <TextView android:layout_width="wrap_content" 

                                    android:layout_height="wrap_content" android:textColor="#ffffffff" 

                                    android:text="音樂" android:textSize="13dp" /> 

                 </LinearLayout> 

                <LinearLayout android:id="@+id/title_phone" 

                          android:paddingLeft="5px" android:paddingRight="5px" 

                          android:layout_width="wrap_content" android:layout_height="wrap_content" 

                          android:orientation="vertical" android:gravity="center" 

                  > 

                          <ImageView android:layout_width="32dip" 

                                   android:layout_height="32dip" android:background="@drawable/title5" 

                                   android:scaleType="centerInside"></ImageView> 

                          <TextView android:layout_width="wrap_content" 

                                   android:layout_height="wrap_content" android:textColor="#ffffffff" 

                                   android:text="電話" 

                                   android:textSize="13dp" /> 

                </LinearLayout> 

                <LinearLayout android:id="@+id/title_author" 

                         android:paddingLeft="5px" 

                         android:layout_width="wrap_content" android:layout_height="wrap_content" 

                         android:orientation="vertical" android:gravity="center" 

                 > 

                         <ImageView android:id="@+id/title_back_image" 

                                 android:background="@drawable/title6" 

                                 android:layout_width="32dip" android:layout_height="32dip" 

                                 android:scaleType="centerInside"> 

                         </ImageView> 

                         <TextView android:id="@+id/title_back_text" 

                                  android:layout_width="wrap_content" android:layout_height="wrap_content" 

                                  android:textColor="#ffffff" android:text="作者" 

                                  android:textSize="13dp" /> 

                </LinearLayout> 

          </LinearLayout> 

    </LinearLayout> 

</LinearLayout> 

 

 2、new_file.xml

 

<?xml version="1.0" encoding="UTF-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

 android:orientation="vertical" android:layout_width="280px" 

 android:layout_height="wrap_content"> 

  

     <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> 

             <LinearLayout android:layout_width="wrap_content" android:layout_height="19dp"></LinearLayout> 

             <LinearLayout android:id="@+id/new_file_text" android:layout_width="280px" 

                       android:layout_height="wrap_content" android:background="@drawable/dialog_title1"> 

                       <TextView android:layout_width="fill_parent" 

                                 android:layout_height="wrap_content" android:layout_gravity="center_vertical" 

                                 android:gravity="center_horizontal" android:text="大明原創" /> 

            </LinearLayout> 

     </LinearLayout> 

     <LinearLayout android:layout_width="fill_parent" 

             android:layout_height="19dp"  

             android:id="@+id/new_file_layout" 

             android:paddingLeft="8dip" 

             android:layout_marginTop="-48dp"> 

      </LinearLayout> 

      <LinearLayout android:id="@+id/LinearLayout_Viewer1" android:layout_marginTop="28dp" 

             android:layout_width="280px" android:layout_height="wrap_content" 

             android:orientation="vertical" 

             android:paddingLeft="10px" 

             android:paddingRight="10px" 

             android:background="@drawable/dialog_body1" 

             android:layout_weight="1.0"> 

            <LinearLayout android:layout_width="fill_parent" 

                     android:layout_height="30dip" android:background="@drawable/word_view" 

                     android:paddingLeft="10dip" android:id="@+id/row1"> 

                    <RelativeLayout android:layout_width="wrap_content" 

                             android:layout_height="wrap_content" android:layout_gravity="center_vertical|center_horizontal"> 

                             <TextView android:layout_width="wrap_content" 

                                       android:layout_height="wrap_content" 

                                       android:layout_centerHorizontal="true" 

                                       android:layout_centerVertical="true" 

                                       android:layout_marginLeft="60dip" 

                                       style="@style/TextFormat" android:id="@+id/text1" /> 

                   </RelativeLayout> 

            </LinearLayout> 

            <LinearLayout android:layout_width="fill_parent" 

                    android:layout_height="2dip" android:background="#ff888888" /> 

                    <LinearLayout android:layout_width="fill_parent" 

                             android:layout_height="30dip" android:background="@drawable/word_view" 

                             android:paddingLeft="10dip" android:id="@+id/row2"> 

                            <RelativeLayout android:layout_width="wrap_content" 

                                     android:layout_height="wrap_content" android:layout_gravity="center_vertical|center_horizontal"> 

                                    <TextView android:layout_width="wrap_content" 

                                              android:layout_height="wrap_content" 

                                              android:layout_centerHorizontal="true" 

                                              android:layout_centerVertical="true" 

                                              android:layout_marginLeft="60dip" 

                                              style="@style/TextFormat" android:id="@+id/text2" /> 

                           </RelativeLayout> 

                   </LinearLayout> 

           </LinearLayout> 

           <LinearLayout android:layout_width="280px" 

                    android:layout_height="wrap_content" android:background="@drawable/dialog_bottom1" /> 

</LinearLayout> 

 

三、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:

 

<?xml version="1.0" encoding="utf-8"?> 

<set xmlns:android="http://schemas.android.com/apk/res/android"> 

    <translate android:duration="800" android:fromYDelta="-100%" 

     android:toYDelta="0.0" /> 

<!–在原地彈出效果–><set 

      android:fromXScale="0.0" android:toXScale="1.0" 

      android:fromYScale="0.0" android:toYScale="1.0" android:pivotX="50%" 

      android:pivotY="50%" android:duration="1000" /> 

       <alpha android:interpolator="@android:anim/decelerate_interpolator" 

        android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="1000" /> 

–> 

</set> 

 

2、ppup_exit.xml

 

<?xml version="1.0" encoding="utf-8"?> 

<set xmlns:android="http://schemas.android.com/apk/res/android"> 

    <scale android:fromXScale="1.0"  

          android:toXScale="0.5" 

          android:fromYScale="1.0"  

          android:toYScale="0.5" 

          android:pivotX="50%"  

          android:pivotY="50%"  

          android:duration="500"/> 

    <alpha android:interpolator="@android:anim/decelerate_interpolator" 

          android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="500"/> 

</set> 

 

五、在Values加入一個style.xml樣式的xml文件:

 

<?xml version="1.0" encoding="utf-8"?> 

<resources> 

 <style name="TextFormat"> 

          <item name="android:textSize">15dip</item> 

          <item name="android:textColor">#ff000000</item> 

 </style> 

  

 <style name="PopupAnimation" parent="android:Animation" mce_bogus="1"> 

            <item name="android:windowEnterAnimation">@anim/popup_enter</item> 

            <item name="android:windowExitAnimation">@anim/popup_exit</item> 

 </style> 

</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>

這是我原創的title的效果,轉載請標明出處:http://blog.csdn.net/wdaming1986/article/details/6636112,

 

謝謝!有需要完整項目的可以留郵箱,我給你發過去!有不明白的地方請留言,我看到會解釋的!

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *