`

android ,点击title的ImageView弹出PopupWindow【安卓进化七】

 
阅读更多

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

 点击MP3这个图标

点击PopupWin苹果MP3这列弹出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>

  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#reply

分享到:
评论
2 楼 warnerhit 2011-12-23  
  <LinearLayout android:layout_width="fill_parent" 
             android:layout_height="19dp"  
             android:id="@+id/new_file_layout" 
             android:paddingLeft="8dip" 
             android:layout_marginTop="-48dp"> 
      </LinearLayout> 
这个,没看java代码的时候看半天不知道干嘛的, android:layout_marginTop="-48dp"为什么要这样用啊
1 楼 warnerhit 2011-12-23  
帮我解决了一个问题,太感谢了。。果断收藏!

相关推荐

Global site tag (gtag.js) - Google Analytics