我有一个视图,它根据布局尺寸(通过ViewTreeObserver)定义其宽度和高度。我有另外两个ImageViews(包含在RelativeLayouts中)。 我需要将这两个图像对齐到第一个视图的顶部和底部。他们这样做,但使用旧的视图尺寸,而不是更正的尺寸:因此,当中心变小时,视图之间存在间隙。
这是我的意思的图像:
代码:
@Override
public void onGlobalLayout() {
layoutWidth = layout.getWidth(); \\ main layout
layoutHeight = layout.getHeight();
LayoutParams params = arrows.getLayoutParams();
params.width = layoutHeight/3;
params.height = layoutHeight/3;
arrows.setLayoutParams(params);
LayoutParams paramseyes = eyes.getLayoutParams();
paramseyes.width = layoutHeight/4;
eyes.setLayoutParams(paramseyes);
mouth.setLayoutParams(paramseyes);
/*
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams
(paramseyes);
lp.addRule(RelativeLayout.ABOVE, R.id.arrows);
eyes.setLayoutParams(lp); ... nah, it didnt work
*/
removeOnGlobalLayoutListener(arrows, this);
}
XML:
<ee.st.running.arrowwidgt
android1:id="@+id/arrows"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_centerInParent="true"
android:dial="@drawable/hourglass1"
android:hand_hour="@drawable/hours"
android:hand_minute="@drawable/minuts"
android:scaleType="fitXY"
android1:src="@drawable/hourglass1" />
<RelativeLayout
android1:id="@+id/eyes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android1:layout_above="@id/arrows" >
<ImageView
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_centerInParent="true"
android1:src="@drawable/eyes" />
</RelativeLayout>
<RelativeLayout
android1:id="@+id/mouthset"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android1:layout_below="@id/arrows"
android:layout_centerHorizontal="true"
>
<ImageView
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_centerHorizontal="true"
android1:src="@drawable/mouth"
/>
</RelativeLayout>
此箭头视图很大(对于多个屏幕支持,我稍后将其缩放到特定大小)。
答案 0 :(得分:0)
我终于意识到了原因。 当我改变布局宽度时,它相应地缩放了图像。但它没有改变布局高度。
我应该做 paramseyes.height = ...
(在我的情况下是layoutHeight / 8;)
所以,问题解决了。