1

我正在使用 Actionbarsherlock,我想PopupWindow在操作栏的正下方放置一个。使用showAtLocation()x 和 y 偏移量,因此理想情况下 y 偏移量将是操作栏的高度。但是当我打电话

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();

我已经翻遍了,找不到类似的问题/答案,那么以前有人经历过吗?谢谢。

编辑:杰克的回答是正确的。为了获得该属性值,我使用了这篇文章

4

3 回答 3

2

actionBarSize您可以从主题属性中读取操作栏的高度。这会根据设备配置而变化,因此请确保在创建或重新创建活动时始终阅读它。

于 2013-03-03T23:54:19.510 回答
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);  

这是一种,我正在尝试其他方法。祝你好运!

是的!我找到了一个非常简单的方法:

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

更多信息,看这个链接

于 2013-08-28T07:51:53.457 回答
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()

于 2013-03-03T23:37:15.170 回答