层次观众做高度的变化出现层次、观众、高度

2023-09-04 06:33:32 作者:暴风雨一样的无敌女人

这是我的第一个问题。

我建立了一个自定义组件:一个 RelativeLayout的的TextView 底部和两个 ImageView的上面说,作为一个直方图的两列可点击的元素。要设置栏的高度,我得到在可用高度 onLayout(),由于容器的高度减去标签的一种:

I have built a custom component: a RelativeLayout with a TextView on the bottom and two ImageView above that, acting as a 2-columns clickable element of an histogram. To set the height of a bar, i get the "available height" in onLayout(), as container's height minus label's one:

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
    mAvailHeight = getHeight()-findViewById(R.id.label).getHeight(); // it works

,然后分配给它(由0-1值乘以)作为布局参数ImageView的:

and then assign it (multiplied by a 0.-1. value) as a layout parameter to the ImageView:

    View bar = findViewById(R.id.bigBar);
    RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) bar.getLayoutParams();
    rlp.height = Math.round((float)mAvailHeight * mBigBarHeight);
}

mBigBarHeight 变量(0-1)可通过此功能进行设置:

The mBigBarHeight variable (0.-1.) can be set via this function:

public void setBigBarHeight(float value, float max) {
    mBigBarHeight = value / max;    
    requestLayout(); //
    invalidate();    // do these help? i find no difference
}

现在。当我在的onCreate加上这些HistogramBar一()并设置高度和标签一切工作如我所料。如果我尝试之后对其进行修改,说onClickSomething:

Now. When i add one of these "HistogramBar" in onCreate() and set the heights and label everything works as I expect. If i try to modify them later, say onClickSomething:

bar.setBigBarHeight(25, 100);
bar.setSmallBarHeight(50, 100);
bar.setLabel("jjk");

只有标签的变化。我检查了层级查看器和实际​​上的LayoutParams没有变化。如果我点击再次发生变化出现。

only the label changes. I checked with Hierarchy Viewer and actually the LayoutParams did change. If i click again changes appear.

有趣的是,就算我做加载视图层次的从工具的的变化得到显示的(仿真器)!!怎么了?是不是很奇怪吗?我想做的事情,在我的code,这样它的作品!

The funny thing is that even if i do "Load View Hierarchy" from the tool changes get displayed (on the emulator)!! What happens? Is it strange? I want to do that in my code so that it works!

我找不到任何类似的问题。谢谢你。

I couldn't find any similar question. Thanks.

推荐答案

在从工具加载层次结构,重新布局/重绘情况来衡量性能。你可能不会调用requestLayout()时,你应该。

When you load a hierarchy from the tool, a relayout/redraw happens to measure performance. You are probably not calling requestLayout() when you should.