[英]Android: alignment after scaling and size changed
I have a view which defines its width and height accordingly to layout dimensions (via ViewTreeObserver). 我有一个视图,它根据布局尺寸(通过ViewTreeObserver)定义其宽度和高度。 I have another two ImageViews (wrapped in the RelativeLayouts).
我还有另外两个ImageViews(包装在RelativeLayouts中)。 I need this two images be aligned to the top and bottom of the first view.
我需要将这两个图像与第一个视图的顶部和底部对齐。 They do, but use old view dimensions, not the corrected ones: so there is a gap between the views, when central gets smaller.
它们可以,但是使用旧的视图尺寸,而不是校正后的尺寸:因此,当中心变小时,视图之间存在间隙。
Here is an image of what I mean: 这是我的意思的图片:
And the code: 和代码:
@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);
}
the XML: 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>
This arrows view is big (for multiple screens support I just scale it to certain size later). 此箭头视图很大(对于多屏支持,我稍后将其缩放到一定大小)。
I finally realized why. 我终于意识到了为什么。 When I changed layout width, it correspondingly scaled the image.
当我更改布局宽度时,它会相应缩放图像。 But it didn't change layout height.
但是它并没有改变布局高度。
I should make paramseyes.height = ... 我应该使paramseyes.height = ...
(In my situation it is layoutHeight/8;) (在我的情况下是layoutHeight / 8;)
So, the problem is solved. 这样,问题就解决了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.