简体   繁体   中英

Android: alignment after scaling and size changed

I have a view which defines its width and height accordingly to layout dimensions (via ViewTreeObserver). I have another two ImageViews (wrapped in the 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:

<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 = ...

(In my situation it is layoutHeight/8;)

So, the problem is solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM