2013-03-03 78 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