`

PopupWindow与PopupMenu的用法

 
阅读更多

转于:http://blog.csdn.net/a379992210/article/details/48423923

------------------------------------------------------------------------------------------

 

PopupWindow与PopupMenu的用法

@(Blog)[马克飞象|Markdown|Android]

 

 

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
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();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

上面的代码最主要的只有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

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
mPopWindowView = getLayoutInflater().inflate(R.layout.pop_window_layout, null);
// Focusable 为True,PopupWindow的点击事件才会相应
mPopWindow = new PopupWindow(mPopWindowView, 150, 120, true);
// 必须在代码中设置一下背景色,点击外面不会隐藏此弹窗
mPopWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
// Focusable 为False时,不执行则点击外面不会隐藏此弹窗
//mPopWindow.setOutsideTouchable(true); 

// 显示 默认显示View的正下方,可以使用重载方法设置偏移量来调整位置
mPopWindow.showAsDropDown(view);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

其实用法和Menu差不多,只是背景色必须要在代码里进行设置,否则点击外面窗体不会消失,不知道这是BUG,还是Google就是这么设计的,有点想不通。

android:textAllCaps=”false” 防止Refresh显示的时候展现为全部大写 
Focusable属性,发现如果设为false的window内的组件就无法收到onClick事件

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics