欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android用PopupWindow實(shí)現(xiàn)自定義overflow

 更新時(shí)間:2016年11月25日 11:14:24   作者:李金堯  
這篇文章主要介紹了Android用PopupWindow實(shí)現(xiàn)自定義overflow的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了PopupWindow實(shí)現(xiàn)自定義overflow的具體代碼,供大家參考,具體內(nèi)容如下

當(dāng)Action Bar的Action放不下時(shí),系統(tǒng)會(huì)將其收集在overflow中。

用hierarchyviewer查看系統(tǒng)自己生成的Overflow,發(fā)現(xiàn)它本身就是popupWindow。

所以我們也可以用popUpWindow來寫自己的overflow實(shí)現(xiàn)更多功能,做出像微信一樣的效果。

第一次寫,廢話有點(diǎn)多,還望多包涵。

效果(GIF演示在文章底部):

最右邊的Action(那個(gè)三點(diǎn)菜單)是自己添加的Action,使用了android開發(fā)包里的圖標(biāo)ic_action_overflow.png,可到官網(wǎng)下載。

首先在Item中添加Action,為了演示,添加了一個(gè)Submenu

<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.popupwindowoverflow.MainActivity" >
<item
    android:id="@+id/action_new"
    android:orderInCategory="1"
    android:title="SubMenu"
    android:icon="@drawable/ic_action_new"
    app:showAsAction="always">
  <menu>
    <item android:id="@+id/submenu1" 
      android:title="Accept" 
      android:titleCondensed="Accept"
      android:icon="@drawable/ic_action_accept" />
     <item android:id="@+id/submenu2" 
      android:title="Cancel" 
      android:titleCondensed="Cancel"
      android:icon="@drawable/ic_action_cancel" />
     <item android:id="@+id/submenu3" 
      android:title="Unread" 
      android:titleCondensed="Unread"
      android:icon="@drawable/ic_action_unread" />
  </menu>
</item>
<item
    android:id="@+id/action_overflow"
    android:orderInCategory="2"
    android:title="PopupWindow"
    android:icon="@drawable/ic_action_overflow"
    app:showAsAction="always"/>

</menu>

監(jiān)聽I(yíng)D為action_overflow的Action,創(chuàng)建popupWindow彈出自己的overflow。

public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.    
    int id = item.getItemId();
    switch (id) {
    case R.id.action_overflow:
      popUpMyOverflow();//彈出自定義overflow
      return true;
    }
    return super.onOptionsItemSelected(item);
  }

 下面介紹popUpMyOverflow()方法,就是通過它彈出了我們的overflow,自定義overflow的布局文件就是R.layout.action_overflow_popwindow,這里就不貼出來啦。

public void popUpMyOverflow() {
    /**
     * 定位PopupWindow,讓它恰好顯示在Action Bar的下方。 通過設(shè)置Gravity,確定PopupWindow的大致位置。
     * 首先獲得狀態(tài)欄的高度,再獲取Action bar的高度,這兩者相加設(shè)置y方向的offset樣PopupWindow就顯示在action
     * bar的下方了。 通過dp計(jì)算出px,就可以在不同密度屏幕統(tǒng)一X方向的offset.但是要注意不要讓背景陰影大于所設(shè)置的offset,
     * 否則陰影的寬度為offset.
     */
    // 獲取狀態(tài)欄高度
    Rect frame = new Rect();
    getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
//    狀態(tài)欄高度:frame.top
    int xOffset = frame.top+getActionBar().getHeight()-25;//減去陰影寬度,適配UI.
    int yOffset = Dp2Px(this, 5f); //設(shè)置x方向offset為5dp
    View parentView = getLayoutInflater().inflate(R.layout.activity_main,
        null);
    View popView = getLayoutInflater().inflate(
        R.layout.action_overflow_popwindow, null);
    PopupWindow popWind = new PopupWindow(popView,
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);//popView即popupWindow的布局,ture設(shè)置focusAble.
    
    //必須設(shè)置BackgroundDrawable后setOutsideTouchable(true)才會(huì)有效。這里在XML中定義背景,所以這里設(shè)置為null;
    popWind.setBackgroundDrawable(new BitmapDrawable(getResources(),
        (Bitmap) null));
    popWind.setOutsideTouchable(true); //點(diǎn)擊外部關(guān)閉。
    popWind.setAnimationStyle(android.R.style.Animation_Dialog);  //設(shè)置一個(gè)動(dòng)畫。
    //設(shè)置Gravity,讓它顯示在右上角。
    popWind.showAtLocation(parentView, Gravity.RIGHT | Gravity.TOP,
        yOffset, xOffset);
  }

在android中,為了適配不同屏幕密度和尺寸,android用了Dp單位,但是在Java代碼中多是接受px單位的尺寸,所以這里要轉(zhuǎn)換一下。

Dp轉(zhuǎn)換Px的方法。

public int Dp2Px(Context context, float dp) {
   final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (dp * scale + 0.5f);
  } 

好的,現(xiàn)在我們有了所有要顯示自定義Overflow的東西了!運(yùn)行你的app吧。

最終效果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論