Actionbarsherlock getHeight() returns 0

自古美人都是妖i 提交于 2019-12-24 09:23:58

问题


I'm using Actionbarsherlock and I want to place a PopupWindow right below the action bar. Using the showAtLocation() takes an x and y offset, so ideally the y offset would be the height of the action bar. But when I call

int abHeight = getSupportActionBar().getHeight();

it returns zero. I'm using a SherlockFragmentActivity

Here's the relevant code:

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

I've looked all over and can't find a similar question/answer, so has anyone experienced this before? Thanks.

EDIT: Jake's answer was right on. In order to get that attribute value I used this post.


回答1:


You can read the height of the action bar from the actionBarSize theme attribute. This changes based on the device configuration so make sure you are always reading it when your activity is created or recreated.




回答2:


in you style.XML add: <item name="@android:attr/actionBarSize">50px</item>

and then in your activity add the following code :

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

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

this is one kind ,I am trying to get other ways.Good luck!

Yeah!I find a way very simple:

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

more info,look this link




回答3:


You can't get the height for views until they have been layed out. Try adding a 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...
    }
});

Refer to the docs starting at View.getViewTreeObserver().



来源:https://stackoverflow.com/questions/15192133/actionbarsherlock-getheight-returns-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!