Frage

I have a RelativeLayout specified as follows

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

On the MainActivity I do this

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 is an extension of a RelativeLayout and is essentially an animated graphical menu. Icons slide in from the side of the screen and spiral around to their positions in a circle around a central graphic.

The center graphic is added in code

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

and later it is moved to the center

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

This works on 4.3 and below. On 4.4 it works about 50% of the time. Half the time getHeight() and getWidth() return 0, and the other half the time they return the correct values. These two functions, unless I'm mistaken, are executed on my PathMenu object, which has a Height and Width of MATCH_PARENT, and it's parent is the first layout I posted with a width of fill_parent and a height of 0dip. I'm compiling my code with a target of 4.4.2, and it runs fine on 4.3 or under devices, but messes up on 4.4.2 devices (it does the same thing when I compile with a target of 4.3 or 4.2 and then run on devices, 4.3 and under work while 4.4.2 messes up).

Why is it messing up on 4.4+ devices and how can I fix it?

War es hilfreich?

Lösung

This is the solution I came up with. If someone has different or better, please post it.

Since the problem only exists in 4.4.2 (and future, I assume), I used an if and the SDK_INT to do something different for 19 and greater. The issue appears to be that images don't have a height and width until displayed, and in 4.4.2 sometimes for a short time after it is displayed the getWidth() and getHeight() functions will still return 0. However, assuming I am doing no scaling on the image, I can use the getBackground().getIntrinsicHeight() and getBackground().getIntrinsicWidth() and use that instead. To get the screen width and height I used getSize(point) on the Display, and since I'm using the screen size and not the size of the Relative View I need to get the padding of the parent in order to center it properly.

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

There is probably a better solution out there, and I hope someone posts it, but this solution is working for me at the moment.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top