【问题标题】:getWidth and getHeight sometimes return 0, sometimes not on 4.4.2getWidth 和 getHeight 有时返回 0,有时在 4.4.2 上不返回
【发布时间】:2015-06-09 03:27:05
【问题描述】:

我有一个如下指定的RelativeLayout

<RelativeLayout
    android:id="@+id/buttons_layout"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:paddingTop="80dip" >
</RelativeLayout>

在 MainActivity 我这样做

mButtonsLayout = (RelativeLayout) findViewById(R.id.buttons_layout);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
mButtonsLayout.addView(new PathMenu(this, entries, this, fullDataPackage.getSnapshot().getUnlistenedCalls()), lp);

PathMenu 是RelativeLayout 的扩展,本质上是一个动画图形菜单。图标从屏幕一侧滑入,并围绕中心图形围绕其位置盘旋。

在代码中添加中心图形

mMenuButton = new ImageButton(getContext());
mMenuButton.setBackgroundResource(R.drawable.selector_refresh_button);
mMenuButton.setOnClickListener(menuButtonClickListener);
addView(mMenuButton, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

然后它被移动到中心

LayoutParams params = (LayoutParams) mMenuButton.getLayoutParams();
params.topMargin = (getHeight() - mMenuButton.getHeight()) / 2;
params.leftMargin = (getWidth() - mMenuButton.getWidth()) / 2;
mMenuButton.setLayoutParams(params);

这适用于 4.3 及以下版本。在 4.4 上,它的工作时间约为 50%。 getHeight() 和 getWidth() 一半时间返回 0,另一半时间返回正确值。除非我弄错了,否则这两个函数是在我的 PathMenu 对象上执行的,它的高度和宽度为 MATCH_PARENT,它的父级是我发布的第一个布局,宽度为 fill_parent,高度为 0dip。我正在使用 4.4.2 的目标编译我的代码,它在 4.3 或设备下运行良好,但在 4.4.2 设备上运行混乱(当我使用 4.3 或 4.2 的目标编译然后在 4.3 和正在工作的设备上运行,而 4.4.2 搞砸了)。

为什么在 4.4+ 设备上会出现问题,我该如何解决?

【问题讨论】:

    标签: android android-layout android-4.4-kitkat


    【解决方案1】:

    这是我想出的解决方案。如果有人有不同或更好的,请发布。

    由于该问题仅存在于 4.4.2(以及未来,我假设)中,我使用 if 和 SDK_INT 为 19 及更高版本做一些不同的事情。问题似乎是图像在显示之前没有高度和宽度,并且在 4.4.2 中有时在显示后的短时间内getWidth() 和getHeight() 函数仍将返回 0。但是,假设我我没有对图像进行缩放,我可以使用 getBackground().getIntrinsicHeight() 和 getBackground().getIntrinsicWidth() 并改用它。为了获得屏幕宽度和高度,我在显示器上使用了getSize(point),由于我使用的是屏幕尺寸而不是相对视图的尺寸,我需要获取父级的填充以使其正确居中。

    LayoutParams params = (LayoutParams) mMenuButton.getLayoutParams();
    if (android.os.Build.VERSION.SDK_INT >= 19) {
      RelativeLayout mButtonsLayout = (RelativeLayout) getParent();
      WindowManager wm = (WindowManager) mcontext.getSystemService(Context.WINDOW_SERVICE);
      Display display = wm.getDefaultDisplay();
      Point size = new Point();
      display.getSize(size);
      params.topMargin = (size.y - mMenuButton.getBackground().getIntrinsicHeight()) / 2 - mButtonsLayout.getPaddingTop();
      params.leftMargin = (size.x - mMenuButton.getBackground().getIntrinsicWidth()) / 2;
    }
    else {
      params.topMargin = (getHeight() - mMenuButton.getHeight()) / 2;
      params.leftMargin = (getWidth() - mMenuButton.getWidth()) / 2;
    }
    mMenuButton.setLayoutParams(params);
    

    那里可能有更好的解决方案,我希望有人发布它,但这个解决方案目前对我有用。

    【讨论】:

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