开发者

Programmatically resize of view depends on android version?

开发者 https://www.devze.com 2023-01-20 23:29 出处:网络
I am facing a weird behavior regarding the subject. I have a very simple layout containing only an empty RelativeLayout.

I am facing a weird behavior regarding the subject.

I have a very simple layout containing only an empty RelativeLayout.

Once I have input form the user this relative layout is filled with square tiles to achieve a mosaic-like effect. Each tile is made by a FrameLayout containing two images (only one is drawn at any given time). It is not possible for me to put the tiles in the XML layout because I do not know in advance how many of them I will need.

In the onSizeChanged of my relative layout, I force a resize on all the tiles to f开发者_如何学运维it the new size.

The code is something like this:

public void resizeTiles(int w, int h) {
    int l1 = w / X; int l2 = h / Y;
    int tileS = (l1 <= l2 ? l1 : l2);
    android.view.ViewGroup.LayoutParams lp;
    for (Tile t : myTiles) {
        lp = t.getLayoutParams();
        lp.width = tileS;
        lp.height = tileS;
    }
}

In my manifest file I have the following:

<uses-sdk 
    android:minSdkVersion="4"
    android:targetSdkVersion="4"
/>

Thus, target system is 1.6.

So far so good, this is working like a charm ... but only on 2.2.

The same binary placed on emulator 2.1-update1 or previous is giving me back an empty layout (I also tried a couple of physical devices, same result).

Debugging, I tracked down the problem is in the resize; commenting out width and height assignments I see the tiles but with distorted proportions.

Any suggestion on how to make this working ?

Thanks in advance.


onSizeChanged() is called late in the layout process, once the final size has been determined. There may already have been some layout passes that have happened down through the hierarchy at that point.

Basic answer is: layout params are for telling the view's parent its layout params prior to is performing a layout. If they change, the layout must be invalidated to perform a new complete layout with the new params. You should never change these in the middle of a layout.

The best thing to do is just write your own layout manager. (If you are doing layout of tiles, RelativeLayout does a ton more stuff that you don't need, making it a lot less efficient than necessary.) It is actually not very hard to write a simple layout manager.

You can use the simple layout managers in the framework as a guide:

https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/AbsoluteLayout.java

https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/FrameLayout.java

(Note that even these are a lot more complicated than you probably need, since they are intended to be general purpose for the layouts they are implementing. We really should have an API demo of a custom layout that is truly simple.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号