android 自定义下拉菜单

    本实例的自定义下拉菜单主要是继承PopupWindow类来实现的弹出窗体,各种布局效果可以根据自己定义设计。弹出的动画效果主要用到了translate、alpha、scale,具体实现步骤如下:

         先上效果图如下:左边下拉菜单、中间下拉菜单、右边下拉菜单

      
          

1.主界面布局 activity_main.xml:

[html] view
plain
 copy

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#ffffff" >  
  6.   
  7.     <include  
  8.         android:id="@+id/main_top"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         layout="@layout/urm_top" />  
  12.   
  13.     <TextView  
  14.         android:id="@+id/rule_line_tv"  
  15.         android:layout_width="match_parent"  
  16.         android:layout_height="0.5dp"  
  17.         android:layout_below="@id/main_top"  
  18.         android:background="@color/reserve_line" />  
  19.   
  20.     <LinearLayout  
  21.         android:id="@+id/main_ll"  
  22.         android:layout_width="match_parent"  
  23.         android:layout_height="wrap_content"  
  24.         android:layout_below="@id/rule_line_tv"  
  25.         android:gravity="center_vertical"  
  26.         android:orientation="horizontal"  
  27.         android:padding="10dp" >  
  28.   
  29.         <TextView  
  30.             android:id="@+id/left_tv"  
  31.             android:layout_width="0dp"  
  32.             android:layout_height="wrap_content"  
  33.             android:layout_weight="1"  
  34.             android:ellipsize="end"  
  35.             android:gravity="center_horizontal"  
  36.             android:maxLength="4"  
  37.             android:singleLine="true"  
  38.             android:text="我负责的线索" />  
  39.   
  40.         <TextView  
  41.             android:id="@+id/middle_tv"  
  42.             android:layout_width="0dp"  
  43.             android:layout_height="wrap_content"  
  44.             android:layout_weight="1"  
  45.             android:ellipsize="end"  
  46.             android:gravity="center_horizontal"  
  47.             android:maxLength="4"  
  48.             android:singleLine="true"  
  49.             android:text="团队" />  
  50.   
  51.         <TextView  
  52.             android:id="@+id/right_tv"  
  53.             android:layout_width="0dp"  
  54.             android:layout_height="wrap_content"  
  55.             android:layout_weight="1"  
  56.             android:ellipsize="end"  
  57.             android:gravity="center_horizontal"  
  58.             android:maxLength="4"  
  59.             android:singleLine="true"  
  60.             android:text="自定义" />  
  61.     </LinearLayout>  
  62.   
  63.     <TextView  
  64.         android:id="@+id/rule_line01_tv"  
  65.         android:layout_width="match_parent"  
  66.         android:layout_height="0.5dp"  
  67.         android:layout_below="@id/main_ll"  
  68.         android:background="@color/reserve_line" />  
  69.   
  70.     <TextView  
  71.         android:id="@+id/main_tv"  
  72.         android:layout_width="wrap_content"  
  73.         android:layout_height="wrap_content"  
  74.         android:layout_centerInParent="true"  
  75.         android:text="主界面" />  
  76.   
  77. </RelativeLayout>  

2.主界面测试类 MainActivity.java

[java] view
plain
 copy

  1. package com.popuptest;  
  2.   
  3. import java.util.ArrayList;  
  4. import android.os.Bundle;  
  5. import android.util.DisplayMetrics;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.view.Window;  
  9. import android.widget.AdapterView;  
  10. import android.widget.Button;  
  11. import android.widget.ImageButton;  
  12. import android.widget.ImageView;  
  13. import android.widget.LinearLayout;  
  14. import android.widget.TextView;  
  15. import android.widget.AdapterView.OnItemClickListener;  
  16. import android.widget.RelativeLayout.LayoutParams;  
  17. import android.app.Activity;  
  18.   
  19. public class MainActivity extends Activity implements OnClickListener {  
  20.   
  21.     public static int screenW, screenH;  
  22.   
  23.     private ImageButton backBtn, createBtn;  
  24.     private Button confirmBtn;  
  25.     private TextView topTv;  
  26.     private LinearLayout topll;  
  27.     private ImageView topIv;  
  28.     private TextView topLineTv;  
  29.   
  30.     private TopMiddlePopup middlePopup;  
  31.   
  32.     @Override  
  33.     protected void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);  
  35.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  36.         setContentView(R.layout.activity_main);  
  37.         getScreenPixels();  
  38.         initWidget();  
  39.     }  
  40.   
  41.     /** 
  42.      * 初始化控件 
  43.      */  
  44.     private void initWidget() {  
  45.         backBtn = (ImageButton) findViewById(R.id.urm_back_btn);  
  46.         createBtn = (ImageButton) findViewById(R.id.urm_create_btn);  
  47.         confirmBtn = (Button) findViewById(R.id.urm_confirm_btn);  
  48.   
  49.         topll = (LinearLayout) findViewById(R.id.urm_top_ll);  
  50.         topIv = (ImageView) findViewById(R.id.urm_top_iv);  
  51.   
  52.         topLineTv = (TextView) findViewById(R.id.rule_line_tv);  
  53.   
  54.         topTv = (TextView) findViewById(R.id.urm_top_tv);  
  55.         topTv.setText("企业客户");  
  56.   
  57.         backBtn.setOnClickListener(this);  
  58.         createBtn.setOnClickListener(this);  
  59.         confirmBtn.setOnClickListener(this);  
  60.         topll.setOnClickListener(this);  
  61.   
  62.     }  
  63.   
  64.     /** 
  65.      * 设置弹窗 
  66.      *  
  67.      * @param type 
  68.      */  
  69.     private void setPopup(int type) {  
  70.         middlePopup = new TopMiddlePopup(MainActivity.this, screenW, screenH,  
  71.                 onItemClickListener, getItemsName(), type);  
  72.     }  
  73.   
  74.     /** 
  75.      * 设置弹窗内容 
  76.      *  
  77.      * @return 
  78.      */  
  79.     private ArrayList<String> getItemsName() {  
  80.         ArrayList<String> items = new ArrayList<String>();  
  81.         items.add("企业客户");  
  82.         items.add("集团客户");  
  83.         items.add("公海客户");  
  84.         return items;  
  85.     }  
  86.   
  87.     @Override  
  88.     public void onClick(View v) {  
  89.         switch (v.getId()) {  
  90.         case R.id.urm_back_btn:  
  91.             setPopup(1);  
  92.             middlePopup.show(topLineTv);  
  93.             break;  
  94.         case R.id.urm_create_btn:  
  95.             setPopup(2);  
  96.             middlePopup.show(topLineTv);  
  97.             break;  
  98.         case R.id.urm_confirm_btn:  
  99.   
  100.             break;  
  101.         case R.id.urm_top_ll:  
  102.             setPopup(0);  
  103.             middlePopup.show(topLineTv);  
  104.             break;  
  105.         }  
  106.     }  
  107.   
  108.     /** 
  109.      * 弹窗点击事件 
  110.      */  
  111.     private OnItemClickListener onItemClickListener = new OnItemClickListener() {  
  112.   
  113.         @Override  
  114.         public void onItemClick(AdapterView<?> parent, View view, int position,  
  115.                 long id) {  
  116.             System.out.println("--onItemClickListener--:");  
  117.             middlePopup.dismiss();  
  118.         }  
  119.     };  
  120.   
  121.     /** 
  122.      * 获取屏幕的宽和高 
  123.      */  
  124.     public void getScreenPixels() {  
  125.         DisplayMetrics metrics = new DisplayMetrics();  
  126.         getWindowManager().getDefaultDisplay().getMetrics(metrics);  
  127.         screenW = metrics.widthPixels;  
  128.         screenH = metrics.heightPixels;  
  129.     }  
  130.   
  131. }  

3.自定义弹窗类 TopMiddlePopup.java

[java] view
plain
 copy

  1. package com.popuptest;  
  2.   
  3. import java.util.ArrayList;  
  4. import android.content.Context;  
  5. import android.graphics.drawable.ColorDrawable;  
  6. import android.view.LayoutInflater;  
  7. import android.view.MotionEvent;  
  8. import android.view.View;  
  9. import android.view.View.OnTouchListener;  
  10. import android.view.ViewGroup.LayoutParams;  
  11. import android.widget.LinearLayout;  
  12. import android.widget.ListView;  
  13. import android.widget.PopupWindow;  
  14. import android.widget.AdapterView.OnItemClickListener;  
  15.   
  16. public class TopMiddlePopup extends PopupWindow {  
  17.   
  18.     private Context myContext;  
  19.     private ListView myLv;  
  20.     private OnItemClickListener myOnItemClickListener;  
  21.     private ArrayList<String> myItems;  
  22.     private int myWidth;  
  23.     private int myHeight;  
  24.     private int myType;  
  25.   
  26.     // 判断是否需要添加或更新列表子类项  
  27.     private boolean myIsDirty = true;  
  28.   
  29.     private LayoutInflater inflater = null;  
  30.     private View myMenuView;  
  31.   
  32.     private LinearLayout popupLL;  
  33.   
  34.     private PopupAdapter adapter;  
  35.   
  36.     public TopMiddlePopup(Context context) {  
  37.         // TODO Auto-generated constructor stub  
  38.     }  
  39.   
  40.     public TopMiddlePopup(Context context, int width, int height,  
  41.             OnItemClickListener onItemClickListener, ArrayList<String> items,  
  42.             int type) {  
  43.   
  44.         inflater = (LayoutInflater) context  
  45.                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  46.         myMenuView = inflater.inflate(R.layout.top_popup, null);  
  47.   
  48.         this.myContext = context;  
  49.         this.myItems = items;  
  50.         this.myOnItemClickListener = onItemClickListener;  
  51.         this.myType = type;  
  52.   
  53.         this.myWidth = width;  
  54.         this.myHeight = height;  
  55.   
  56.         System.out.println("--myWidth--:" + myWidth + "--myHeight--:"  
  57.                 + myHeight);  
  58.         initWidget();  
  59.         setPopup();  
  60.     }  
  61.   
  62.     /** 
  63.      * 初始化控件 
  64.      */  
  65.     private void initWidget() {  
  66.         myLv = (ListView) myMenuView.findViewById(R.id.popup_lv);  
  67.         popupLL = (LinearLayout) myMenuView.findViewById(R.id.popup_layout);  
  68.         myLv.setOnItemClickListener(myOnItemClickListener);  
  69.   
  70.         if (myType == 1) {  
  71.             android.widget.RelativeLayout.LayoutParams lpPopup = (android.widget.RelativeLayout.LayoutParams) popupLL  
  72.                     .getLayoutParams();  
  73.             lpPopup.width = (int) (myWidth * 1.0 / 4);  
  74.             lpPopup.setMargins(0, 0, (int) (myWidth * 3.0 / 4), 0);  
  75.             popupLL.setLayoutParams(lpPopup);  
  76.         } else if (myType == 2) {  
  77.             android.widget.RelativeLayout.LayoutParams lpPopup = (android.widget.RelativeLayout.LayoutParams) popupLL  
  78.                     .getLayoutParams();  
  79.             lpPopup.width = (int) (myWidth * 1.0 / 4);  
  80.             lpPopup.setMargins((int) (myWidth * 3.0 / 4), 0, 0, 0);  
  81.             popupLL.setLayoutParams(lpPopup);  
  82.         }  
  83.     }  
  84.   
  85.     /** 
  86.      * 设置popup的样式 
  87.      */  
  88.     private void setPopup() {  
  89.         // 设置AccessoryPopup的view  
  90.         this.setContentView(myMenuView);  
  91.         // 设置AccessoryPopup弹出窗体的宽度  
  92.         this.setWidth(LayoutParams.MATCH_PARENT);  
  93.         // 设置AccessoryPopup弹出窗体的高度  
  94.         this.setHeight(LayoutParams.MATCH_PARENT);  
  95.         // 设置AccessoryPopup弹出窗体可点击  
  96.         this.setFocusable(true);  
  97.         // 设置AccessoryPopup弹出窗体的动画效果  
  98.         if (myType == 1) {  
  99.             this.setAnimationStyle(R.style.AnimTopLeft);  
  100.         } else if (myType == 2) {  
  101.             this.setAnimationStyle(R.style.AnimTopRight);  
  102.         } else {  
  103.             //this.setAnimationStyle(R.style.AnimTop);  
  104.             this.setAnimationStyle(R.style.AnimTopMiddle);  
  105.         }  
  106.         // 实例化一个ColorDrawable颜色为半透明  
  107.         ColorDrawable dw = new ColorDrawable(0x33000000);  
  108.         // 设置SelectPicPopupWindow弹出窗体的背景  
  109.         this.setBackgroundDrawable(dw);  
  110.   
  111.         myMenuView.setOnTouchListener(new OnTouchListener() {  
  112.   
  113.             @Override  
  114.             public boolean onTouch(View v, MotionEvent event) {  
  115.   
  116.                 int height = popupLL.getBottom();  
  117.                 int left = popupLL.getLeft();  
  118.                 int right = popupLL.getRight();  
  119.                 System.out.println("--popupLL.getBottom()--:"  
  120.                         + popupLL.getBottom());  
  121.                 int y = (int) event.getY();  
  122.                 int x = (int) event.getX();  
  123.                 if (event.getAction() == MotionEvent.ACTION_UP) {  
  124.                     if (y > height || x < left || x > right) {  
  125.                         System.out.println("---点击位置在列表下方--");  
  126.                         dismiss();  
  127.                     }  
  128.                 }  
  129.                 return true;  
  130.             }  
  131.         });  
  132.     }  
  133.   
  134.     /** 
  135.      * 显示弹窗界面 
  136.      *  
  137.      * @param view 
  138.      */  
  139.     public void show(View view) {  
  140.         if (myIsDirty) {  
  141.             myIsDirty = false;  
  142.             adapter = new PopupAdapter(myContext, myItems, myType);  
  143.             myLv.setAdapter(adapter);  
  144.         }  
  145.   
  146.         showAsDropDown(view, 0, 0);  
  147.     }  
  148.   
  149. }  

4.自定义弹窗布局 top_popup.xml

[html] view
plain
 copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <LinearLayout  
  7.         android:id="@+id/popup_layout"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_alignParentTop="true"  
  11.         android:background="#ffffff"  
  12.         android:orientation="vertical" >  
  13.   
  14.         <ListView  
  15.             android:id="@+id/popup_lv"  
  16.             android:layout_width="match_parent"  
  17.             android:layout_height="match_parent"  
  18.             android:divider="@color/content_line"  
  19.             android:dividerHeight="0.5dp" >  
  20.         </ListView>  
  21.   
  22.         <TextView  
  23.             android:layout_width="match_parent"  
  24.             android:layout_height="0.5dp"  
  25.             android:background="@color/reserve_line" />  
  26.     </LinearLayout>  
  27.   
  28. </RelativeLayout>  

5.弹窗类表适配器类 PopupAdapter

[java] view
plain
 copy

  1. package com.popuptest;  
  2.   
  3. import java.util.ArrayList;  
  4. import android.content.Context;  
  5. import android.view.Gravity;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.BaseAdapter;  
  10. import android.widget.RelativeLayout.LayoutParams;  
  11. import android.widget.TextView;  
  12.   
  13. public class PopupAdapter extends BaseAdapter {  
  14.     private Context myContext;  
  15.     private LayoutInflater inflater;  
  16.     private ArrayList<String> myItems;  
  17.     private int myType;  
  18.   
  19.     public PopupAdapter(Context context, ArrayList<String> items, int type) {  
  20.         this.myContext = context;  
  21.         this.myItems = items;  
  22.         this.myType = type;  
  23.   
  24.         inflater = LayoutInflater.from(myContext);  
  25.   
  26.     }  
  27.   
  28.     @Override  
  29.     public int getCount() {  
  30.         return myItems.size();  
  31.     }  
  32.   
  33.     @Override  
  34.     public String getItem(int position) {  
  35.         return myItems.get(position);  
  36.     }  
  37.   
  38.     @Override  
  39.     public long getItemId(int position) {  
  40.         return 0;  
  41.     }  
  42.   
  43.     @Override  
  44.     public View getView(int position, View convertView, ViewGroup parent) {  
  45.         PopupHolder holder = null;  
  46.         if (convertView == null) {  
  47.             holder = new PopupHolder();  
  48.             convertView = inflater.inflate(R.layout.top_popup_item, null);  
  49.             holder.itemNameTv = (TextView) convertView  
  50.                     .findViewById(R.id.popup_tv);  
  51.             if (myType == 0) {  
  52.                 holder.itemNameTv.setGravity(Gravity.CENTER);  
  53.             } else if (myType == 1) {  
  54.                 holder.itemNameTv.setGravity(Gravity.LEFT);  
  55.             } else if (myType == 2) {  
  56.                 holder.itemNameTv.setGravity(Gravity.RIGHT);  
  57.             }  
  58.             convertView.setTag(holder);  
  59.         } else {  
  60.             holder = (PopupHolder) convertView.getTag();  
  61.         }  
  62.         String itemName = getItem(position);  
  63.         holder.itemNameTv.setText(itemName);  
  64.         return convertView;  
  65.     }  
  66.   
  67.     private class PopupHolder {  
  68.         TextView itemNameTv;  
  69.     }  
  70.   
  71. }  

6.子item布局 top_popup_item.xml

[html] view
plain
 copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:background="#ffffff"  
  6.     android:padding="10dp" >  
  7.   
  8.     <TextView  
  9.         android:id="@+id/popup_tv"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"   
  12.         style="@style/urm_tv"/>  
  13.   
  14. </RelativeLayout>  

7.主界面顶部布局 urm_top.xml

[html] view
plain
 copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:background="#eeeeee" >  
  6.   
  7.     <ImageButton  
  8.         android:id="@+id/urm_back_btn"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:background="@null"  
  13.         android:contentDescription="@string/app_name"  
  14.         android:src="@drawable/back" />  
  15.   
  16.     <LinearLayout  
  17.         android:id="@+id/urm_top_ll"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_centerInParent="true"  
  21.         android:gravity="center_vertical"  
  22.         android:orientation="horizontal" >  
  23.   
  24.         <TextView  
  25.             android:id="@+id/urm_top_tv"  
  26.             style="@style/main_tv_style"  
  27.             android:layout_width="wrap_content"  
  28.             android:layout_height="wrap_content"  
  29.             android:text="企业客户" />  
  30.   
  31.         <ImageView  
  32.             android:id="@+id/urm_top_iv"  
  33.             android:layout_width="wrap_content"  
  34.             android:layout_height="wrap_content"  
  35.             android:layout_marginLeft="5dp"  
  36.             android:background="@null"  
  37.             android:contentDescription="@string/app_name"  
  38.             android:src="@drawable/switch02" />  
  39.     </LinearLayout>  
  40.   
  41.     <RelativeLayout  
  42.         android:id="@+id/urm_top_right_rl"  
  43.         android:layout_width="wrap_content"  
  44.         android:layout_height="wrap_content"  
  45.         android:layout_alignParentRight="true"  
  46.         android:layout_centerVertical="true" >  
  47.   
  48.         <ImageButton  
  49.             android:id="@+id/urm_create_btn"  
  50.             android:layout_width="wrap_content"  
  51.             android:layout_height="wrap_content"  
  52.             android:background="@null"  
  53.             android:contentDescription="@string/app_name"  
  54.             android:src="@drawable/btn_add_2x" />  
  55.   
  56.         <Button  
  57.             android:id="@+id/urm_confirm_btn"  
  58.             android:layout_width="wrap_content"  
  59.             android:layout_height="wrap_content"  
  60.             android:background="@null"  
  61.             android:gravity="center_vertical"  
  62.             android:padding="10dp"  
  63.             android:text="确定"  
  64.             android:textColor="@color/blue2"  
  65.             android:textSize="18sp"  
  66.             android:visibility="gone" />  
  67.     </RelativeLayout>  
  68.   
  69.     <ImageButton  
  70.         android:id="@+id/urm_search_btn"  
  71.         android:layout_width="wrap_content"  
  72.         android:layout_height="wrap_content"  
  73.         android:layout_centerVertical="true"  
  74.         android:layout_toLeftOf="@id/urm_top_right_rl"  
  75.         android:background="@null"  
  76.         android:contentDescription="@string/app_name"  
  77.         android:src="@drawable/search"  
  78.         android:visibility="gone" />  
  79.   
  80. </RelativeLayout>  

8.styles.xml文件

[html] view
plain
 copy

  1. <resources>  
  2.   
  3.     <!--  
  4.         Base application theme, dependent on API level. This theme is replaced  
  5.         by AppBaseTheme from res/values-vXX/styles.xml on newer devices.  
  6.     -->  
  7.     <style name="AppBaseTheme" parent="android:Theme.Light">  
  8.         <!--  
  9.             Theme customizations available in newer API levels can go in  
  10.             res/values-vXX/styles.xml, while customizations related to  
  11.             backward-compatibility can go here.  
  12.         -->  
  13.     </style>  
  14.   
  15.     <!-- Application theme. -->  
  16.     <style name="AppTheme" parent="AppBaseTheme">  
  17.         <!-- All customizations that are NOT specific to a particular API-level can go here. -->  
  18.     </style>  
  19.       
  20.     <style name="AnimTop" parent="@android:style/Animation">  
  21.         <item name="android:windowEnterAnimation">@anim/push_top_in</item>  
  22.         <item name="android:windowExitAnimation">@anim/push_top_out</item>  
  23.     </style>  
  24.       
  25.      <style name="AnimTopRight" parent="@android:style/Animation">  
  26.         <item name="android:windowEnterAnimation">@anim/top_right_in</item>  
  27.         <item name="android:windowExitAnimation">@anim/top_right_out</item>  
  28.     </style>  
  29.       
  30.      <style name="AnimTopLeft" parent="@android:style/Animation">  
  31.         <item name="android:windowEnterAnimation">@anim/top_left_in</item>  
  32.         <item name="android:windowExitAnimation">@anim/top_left_out</item>  
  33.     </style>  
  34.       
  35.      <style name="AnimTopMiddle" parent="@android:style/Animation">  
  36.         <item name="android:windowEnterAnimation">@anim/top_middle_in</item>  
  37.         <item name="android:windowExitAnimation">@anim/top_middle_out</item>  
  38.     </style>  
  39.       
  40.     <style name="main_tv_style">  
  41.         <item name="android:textSize">20sp</item>  
  42.         <item name="android:textColor">#000000</item>  
  43.     </style>  
  44.   
  45.     <style name="urm_tv">  
  46.         <item name="android:textSize">18sp</item>  
  47.     </style>  
  48. </resources>  

9.各种动画效果

push_top_in.xml

[html] view
plain
 copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- 从屏幕上面进入 -->  
  3. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  4.   
  5.     <translate  
  6.         android:duration="500"  
  7.         android:fromYDelta="-100%p"  
  8.         android:toYDelta="0" />  
  9.   
  10.     <alpha  
  11.         android:duration="500"  
  12.         android:fromAlpha="0.0"  
  13.         android:toAlpha="1.0" />  
  14.   
  15. </set>  

push_top_out.xml

[html] view
plain
 copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- 从屏幕上面退出 -->  
  3. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  4.   
  5.     <translate  
  6.         android:duration="500"  
  7.         android:fromYDelta="0"  
  8.         android:toYDelta="-100%p" />  
  9.   
  10.     <alpha  
  11.         android:duration="500"  
  12.         android:fromAlpha="1.0"  
  13.         android:toAlpha="0.0" />  
  14.   
  15. </set>  

top_left_in.xml

[html] view
plain
 copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <scale  
  5.         android:duration="500"  
  6.         android:fillAfter="false"  
  7.         android:fromXScale="0.0"  
  8.         android:fromYScale="0.0"  
  9.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  10.         android:pivotX="0%"  
  11.         android:pivotY="0%"  
  12.         android:toXScale="1.0"  
  13.         android:toYScale="1.0" />  
  14.   
  15. </set>  

top_left_out.xml

[html] view
plain
 copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <scale  
  5.         android:duration="500"  
  6.         android:fillAfter="false"  
  7.         android:fromXScale="1.0"  
  8.         android:fromYScale="1.0"  
  9.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  10.         android:pivotX="0%"  
  11.         android:pivotY="0%"  
  12.         android:toXScale="0.0"  
  13.         android:toYScale="0.0" />  
  14.   
  15. </set>  

top_middle_in.xml

[html] view
plain
 copy

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

top_middle_out.xml

[html] view
plain
 copy

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

top_right_in.xml

[html] view
plain
 copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <scale  
  5.         android:duration="500"  
  6.         android:fillAfter="false"  
  7.         android:fromXScale="0.0"  
  8.         android:fromYScale="0.0"  
  9.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  10.         android:pivotX="100%"  
  11.         android:pivotY="0%"  
  12.         android:toXScale="1.0"  
  13.         android:toYScale="1.0" />  
  14.   
  15. </set>  

top_right_out.xml

[html] view
plain
 copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <scale  
  5.         android:duration="500"  
  6.         android:fillAfter="false"  
  7.         android:fromXScale="1.0"  
  8.         android:fromYScale="1.0"  
  9.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  10.         android:pivotX="100%"  
  11.         android:pivotY="0%"  
  12.         android:toXScale="0.0"  
  13.         android:toYScale="0.0" />  
  14.   
  15. </set>  

运行项目即可搞定!

时间: 2024-08-28 12:12:31

android 自定义下拉菜单的相关文章

Android编程下拉菜单spinner用法小结(附2则示例)_Android

本文较为详细的总结分析了Android编程下拉菜单spinner用法.分享给大家供大家参考,具体如下: Spinner控件也是一种列表类型的控件,它的继承关系如下: java.lang.Object    ↳ android.view.View      ↳ android.view.ViewGroup        ↳ android.widget.AdapterView<Textends android.widget.Adapter>          ↳ android.widget.A

Android编程下拉菜单spinner用法小结(附2则示例)

本文较为详细的总结分析了Android编程下拉菜单spinner用法.分享给大家供大家参考,具体如下: Spinner控件也是一种列表类型的控件,它的继承关系如下: java.lang.Object    ↳ android.view.View      ↳ android.view.ViewGroup        ↳ android.widget.AdapterView<Textends android.widget.Adapter>          ↳ android.widget.A

求大神 gridview中关于自定义下拉菜单的联动如何实现

问题描述 [size=14px]如题,gridview中自定义下拉菜单的联动如何实现,困扰我很久了,方法很多,因为自己确实是个菜鸟,搞不定.哪位大神能贴个代码吗?我需要设定"维护班组"与"变电所"的联动下拉.比如维护班组中有一班.二班.三班下拉菜单,需要对应变电所的1#变.2#变.3#变,4#变.5#变.6#变,7#变.8#变.9#变,这几个[/size] 解决方案

android自定义下拉刷新的头部

问题描述 android自定义下拉刷新的头部 用PullToRefresh的时候,自带的是一个"下拉刷新"提示,我想改一下这个文字,怎么弄啊 还有那个图标 解决方案 已解决 ILoadingLayout startLabels= listview.getLoadingLayoutProxy(); // startLabels.setPullLabel("下拉刷新...");// 刚下拉时,显示的提示 // startLabels.setRefreshingLabel

Android自定义下拉刷新上拉加载_Android

本文实例为大家分享了Android自定义下拉刷新上拉加载的具体实现步骤,供大家参考,具体内容如下 实现的方式是SwipeRefreshLayout + RecyclerView 的VIewType 首先看效果: 总的思路: 布局文件 <android.support.v4.widget.SwipeRefreshLayout android:layout_marginTop="?attr/actionBarSize" android:id="@+id/one_refres

Android实现下拉菜单Spinner效果_Android

Android 中下拉菜单,即如html中的<select>,关键在于调用setDropDownViewResource方法,以XML的方式定义下拉菜单要显示的模样 1.1.activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android

Android自定义下拉刷新上拉加载

本文实例为大家分享了Android自定义下拉刷新上拉加载的具体实现步骤,供大家参考,具体内容如下 实现的方式是SwipeRefreshLayout + RecyclerView 的VIewType 首先看效果: 总的思路: 布局文件 <android.support.v4.widget.SwipeRefreshLayout android:layout_marginTop="?attr/actionBarSize" android:id="@+id/one_refres

Android Spinner 下拉菜单的使用_Android

Android 中下拉菜单,即如html中的<select>,关键在于调用setDropDownViewResource方法,以XML的方式定义下拉菜单要显示的模样 步骤: 1.定义Spinner控件 复制代码 代码如下: <Spinner android:id="@+id/spinner"android:layout_width="fill_parent"android:layout_height="wrap_content"

Android自定义下拉刷新控件RefreshableView_Android

这是在了解下拉刷新功能原理下的产物,下拉刷新可以说是国产APP里面必有的功能,连Google都为此出了SwipeRefreshLayout,一种MD风格的下拉刷新. 不过,MD风格在国内似乎很是艰难,不单单是国内系统主流仍是4.4的原因,也有用户习惯的问题,扯的有点多了,在看了许多博客之后,我突然想写一个能仿照 SwipeRefreshLayout 的兼容所有控件的下拉刷新,不单单只是 ListView,希望它也可以包容普通的View和ScrollView,经过两天的奋斗,终于搞定了,因为我的目