仿微信-界面动画(2)

这次我们还是继续研究整个开发中遇到的一些有意思,值得一看的小实例

1.ViewPager中存在按钮或者图片(效果是点击进入ViewPager的各个View),如何实现呢?

.再次,先说明:view存在ViewPager中的顺序是0-1-2-3-4.......

.mTab1.setOnClickListener(new MyOnClickListener(0));
         mTab2.setOnClickListener(new MyOnClickListener(1));
         mTab3.setOnClickListener(new MyOnClickListener(2));
         mTab4.setOnClickListener(new MyOnClickListener(3));

.

public class MyOnClickListener implements View.OnClickListener {
private int index = 0;
public MyOnClickListener(int i) {
index = i;
}
@Override
public void onClick(View v) {
viewPager.setCurrentItem(index);
}
};

.

2.PopUpMenue的使用---一种弹出菜单----效果是微信或者QQ右上角按钮点击会展开多个选项的菜单栏

.

在activity或者fragment中使用PopupMenu

  
  
  1.        final  ImageButton moreMenu = (ImageButton)v.findViewById(R.id.more_menu);
  2.         moreMenu.setOnClickListener(new OnClickListener() {
  3.             @Override
  4.             public void onClick(View v){
  5.                    PopupMenu popup = new PopupMenu(getActivity(), moreMenu);
  6.                     //Inflating the Popup using xml file
  7.                     popup.getMenuInflater()
  8.                         .inflate(R.menu.poupup_menu_home, popup.getMenu());
  9.  
  10.                     //registering popup with OnMenuItemClickListener
  11.                     popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
  12.                         public boolean onMenuItemClick(MenuItem item) {
  13.                   
  14.                             return true;
  15.                         }
  16.                     });
  17.  
  18.                     popup.show(); //showing popup menu 
  19.             }
  20.         });

其中moreMenu是一个按钮,由他触发PopupMenu的显示。

.

//该方法返回的是一个View的对象,是布局中的根
layout = inflater.inflate(R.layout.main_menu, null);

//下面我们要考虑了,我怎样将我的layout加入到PopupWindow中呢???很简单
menuWindow = new PopupWindow(layout,android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT); //后两个参数是width和height
//menuWindow.showAsDropDown(layout); //设置弹出效果
//menuWindow.showAsDropDown(null, 0, layout.getHeight());
menuWindow.showAtLocation(this.findViewById(R.id.mainweixin), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置

.

. 下面是PopupMenu以及PopupWindow的区别:

.

PopupWindowPopupMenu的功能都是为了弹出一个窗体,不过PopupMenu的功能比较单一,而PopupWindow更强。

PopupMenu

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.focus.androidnote.popupwindow.PopupWindowActivity">
    <item 
        android:id="@+id/action_refresh" 
        android:title="@string/action_refresh"
        android:orderInCategory="100" 
        app:showAsAction="never" />
    <item android:id="@+id/action_settings" 
        android:title="@string/action_settings"
        android:orderInCategory="100" 
        app:showAsAction="never" />
</menu>

.

.
public class PopupWindowActivity extends BaseActivity {

    private Button mPopWindowBtn;
    private Button mPopMenuBtn;

    private View mPopWindowView;
    private PopupWindow mPopWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popup_window);

        mPopWindowBtn = (Button) findViewById(R.id.pop_window_btn);
        mPopMenuBtn = (Button) findViewById(R.id.pop_menu_btn);

        mPopMenu = new PopupMenu(mContext, mPopMenuBtn);
        mPopMenu = new PopupMenu(mContext, mPopMenuBtn);
        mPopMenu.getMenuInflater().inflate(R.menu.menu_popup_window, mPopMenu.getMenu());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_popup_window, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            Toast.makeText(mContext, "Settings", Toast.LENGTH_SHORT).show();
        } else if (id == R.id.action_refresh) {
            Toast.makeText(mContext, "Refresh", Toast.LENGTH_SHORT).show();
        }

        return true;
    }

    public void popMenuBtnOnClick(View view) {
        mPopMenu.show();
    }
}
  
  

    .

    .

    上面的代码最主要的只有3行

    mPopMenu = new PopupMenu(mContext, mPopMenuBtn);
    mPopMenu.getMenuInflater().inflate(R.menu.menu_popup_window, mPopMenu.getMenu());
      
      
    • 1
    • 2

    很简单,只需要两行代码就搞定了。 
    期初我以为PopupMenu会和ActivityMenu共用Click事件的,但是实现onOptionsItemSelected方法后发现只有ActivityMenu会触发事件,mPopMenu依然要通过setOnMenuItemClickListener()才能实现点击事件的监听 
    效果图

    .

    .

    PopupWindow相比menu功能要强的多,可以实现布局更加复杂的效果 
    效果图

    .

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

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

    android:orientation="vertical" 

    android:layout_width="100dp"  

    android:layout_height="120dp"  

    android:background="@drawable/bg_pop_window">  

    <Button  

    android:layout_width="wrap_content"  

    android:layout_height="wrap_content"  

    android:layout_gravity="center_horizontal"  

    android:layout_marginTop="20dp"  

    android:background="@android:color/transparent"  

    android:text="@string/action_settings"  

    android:textColor="@android:color/white"  

    android:textAllCaps="false"/>  

    <View  

    android:layout_width="wrap_content"  

    android:layout_height="2dp"  

    android:background="@color/material_blue_grey_800"/>  

    <Button  

    android:layout_width="wrap_content"  

    android:layout_height="wrap_content"  

    android:layout_gravity="center_horizontal"  

    android:background="@android:color/transparent"  

    android:text="@string/action_refresh"  

    android:textColor="@android:color/white"  

    android:textAllCaps="false"/>

    </LinearLayout>


    .关于第三点的文章,转自http://blog.csdn.net/a379992210/article/details/48423923


    • 1
      点赞
    • 0
      收藏
      觉得还不错? 一键收藏
    • 0
      评论

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

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

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值