以编程方式调整的看法取决于Android版本? [英] Programmatically resize of view depends on android version?

查看:117
本文介绍了以编程方式调整的看法取决于Android版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面对有关问题的怪异行为。

I am facing a weird behavior regarding the subject.

我只有一个非常简单的布局包含一个空的 RelativeLayout的的。

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

在我输入表单的用户的相对布局充满了广场砖,实现了马赛克般的效果。每个瓦片由包含两个图像一个的FrameLayout制成(只有一个被绘制在任何给定时间)。这是不可能的,我把瓦片在XML布局,因为我事先不知道我会多少人需要做的。

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.

在我的亲戚布局的 onSizeChanged 的,我强迫所有的瓷砖的大小调整,以适应新的大小。

In the onSizeChanged of my relative layout, I force a resize on all the tiles to fit the new size.

在code是这样的:

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"
/>

因此​​,目标系统是1.6。

Thus, target system is 1.6.

到目前为止好,这是工作就像一个魅力......但仅限于2.2。

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

同样的二进制文件放在模拟器2.1 UPDATE1或previous是给我回一个空的布局(我也尝试了几个物理设备,同样的结果)。

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 ?

在此先感谢。

推荐答案

onSizeChanged()被调用下旬在布局过程中,一旦最终规模已经确定。有可能已经发生上下贯通的层次结构,在这一点上,一些布局通行证。

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.

基本答案是:​​布局PARAMS是告诉视图的父其布局PARAMS的之前的到正在执行的布局。如果他们改变,布局必须失效与新PARAMS执行新的完整布局。你不应该在布局的中间改变这些。

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.

最好的事情要做的只是写自己的布局管理器。 (如果你是做瓷砖的布局,RelativeLayout的做了的的更多的东西,你并不需要,使其成为了很多不必要的效率低。)它实际上不是很难写一个简单的布局经理。

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:

<一个href="http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/widget/AbsoluteLayout.java" rel="nofollow">http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/widget/AbsoluteLayout.java

<一个href="http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/widget/FrameLayout.java" rel="nofollow">http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/widget/FrameLayout.java

(请注意,即使这是一个复杂得多可能比你需要的,因为他们的目的是因为他们正在实施的布局通用。我们真的应该有一个自定义布局,是真正简单的API演示。)

(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.)

这篇关于以编程方式调整的看法取决于Android版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆