【问题标题】:Actionbarsherlock getHeight() returns 0Actionbarsherlock getHeight() 返回 0
【发布时间】:2014-05-19 14:49:06
【问题描述】:

我正在使用 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();

我已经翻遍了,找不到类似的问题/答案,那么以前有没有人遇到过这种情况?谢谢。

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

【问题讨论】:

    标签: android actionbarsherlock


    【解决方案1】:

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

    【讨论】:

    【解决方案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());
        }
    

    更多信息,看这个link

    【讨论】:

      【解决方案3】:

      在布置视图之前,您无法获得视图的高度。尝试添加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() 开头的文档。

      【讨论】:

        猜你喜欢
        • 2014-03-02
        • 1970-01-01
        • 1970-01-01
        • 2011-12-31
        • 1970-01-01
        • 2017-11-23
        • 1970-01-01
        • 1970-01-01
        • 2011-09-02
        相关资源
        最近更新 更多