2013-03-03 41 views
1

我正在使用Actionbarsherlock,我想在操作欄下方放置一個PopupWindow。使用showAtLocation()需要x和y偏移量,所以理想情況下y偏移量應該是操作欄的高度。但是當我撥打Actionbarsherlock getHeight()返回0

int abHeight = getSupportActionBar().getHeight(); 

它返回零。我使用的是SherlockFragmentActivity

下面是相關代碼:

slidingLayout = inflater.inflate(R.layout.sliding_menu, null); 
menuDrawer = MenuDrawer.attach(this, MenuDrawer.MENU_DRAG_CONTENT, Position.LEFT); 
menuDrawer.setContentView(R.layout.activity_main); 
menuDrawer.setMenuView(slidingLayout.findViewById(R.id.sliding_menu)); 

getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 
getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
int abHeight = getSupportActionBar().getHeight(); 

我看了遍,並不能找到一個類似的問題/答案,所以有沒有人經歷過這個?謝謝。

編輯:傑克的答案是正確的。爲了得到我使用的屬性值this post

回答

2

您可以從actionBarSize主題屬性中讀取操作欄的高度。這會根據設備配置進行更改,因此請確保在創建或重新創建活動時始終閱讀它。

+1

感謝傑克。我用http://stackoverflow.com/a/13216807/1754999來獲得大小,它一切工作。我認爲在某處可能會出現px問題,但我會解決這個問題。 – Wenger 2013-03-04 02:19:46

0

直到佈局完成後,才能獲得視圖的高度。嘗試添加ViewTreeObserver

someView.getViewTreeObserver().addGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
     // Remember to remove it if you don't want it to fire every time 
     someView.getViewTreeObserver().removeGlobalOnLayoutListener(this); 

     int abHeight = getSupportActionBar().getHeight(); 
     // Use the height as desired... 
    } 
}); 

參考文檔開始View.getViewTreeObserver()

2

style.XML添加:<item name="@android:attr/actionBarSize">50px</item>

,然後在活動中添加以下代碼:

TypedArray actionbarSizeTypedArray = mContext.obtainStyledAttributes(new int[] { android.R.attr.actionBarSize}); 

     int h = (int) actionbarSizeTypedArray.getDimension(0, 0); 

這是一種,我試圖讓其他ways.Good運氣! !

是的,我找到一種方法很簡單:

TypedValue tv = new TypedValue(); 
    if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) 
    { 
     int h=TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics()); 
    } 

更多信息,看看這個link

相關問題