如何检测布局调整大小?

时间:2010-11-13 20:47:11

标签: android user-interface layout resize

Dianne Hackborn在几个主题中提到,您可以检测布局何时调整大小,例如,当软键盘打开或关闭时。这样的线程就是这个...... http://groups.google.com/group/android-developers/browse_thread/thread/d318901586313204/2b2c2c7d4bb04e1b

但是,我不理解她的回答:“通过所有相应的布局遍历和回调来调整视图层次结构。”

有没有人有进一步的描述或如何检测这个的一些例子?我可以链接哪些回调来检测这个?

由于

5 个答案:

答案 0 :(得分:50)

覆盖onSizeChanged中的View

答案 1 :(得分:40)

一种方法是查看。addOnLayoutChangeListener。在这种情况下,不需要对视图进行子类化。但是你确实需要API级别11.并且从边界(在API中未记录)正确计算大小有时可能是一个陷阱。这是一个正确的例子:

public void onLayoutChange( View v, int left, int top, int right, int bottom,
  int leftWas, int topWas, int rightWas, int bottomWas )
{
    int widthWas = rightWas - leftWas; // right exclusive, left inclusive
    if( v.getWidth() != widthWas )
    {
        // width has changed
    }
    int heightWas = bottomWas - topWas; // bottom exclusive, top inclusive
    if( v.getHeight() != heightWas )
    {
        // height has changed
    }
}

另一种方式(如dacwe的回答)是对视图进行子类化并覆盖onSizeChanged

答案 2 :(得分:3)

我的解决方案是在布局/片段的末尾添加一个不可见的微小哑视图(或将其添加为背景),因此布局大小的任何更改都将触发该视图的布局更改事件被OnLayoutChangeListener抓住:

将哑视图添加到布局末尾的示例:

 <View 
    android:id="@+id/theDumbViewId"
    android:layout_width="1dp"
    android:layout_height="1dp"
     />

倾听事件:

    View dumbView = mainView.findViewById(R.id.theDumbViewId);
    dumbView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            // Your code about size changed
        }
    });

答案 3 :(得分:1)

使用 Kotlin 扩展:

inline fun View?.onSizeChange(crossinline runnable: () -> Unit) = this?.apply {
    addOnLayoutChangeListener { _, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom ->
        val rect = Rect(left, top, right, bottom)
        val oldRect = Rect(oldLeft, oldTop, oldRight, oldBottom)
        if (rect.width() != oldRect.width() || rect.height() != oldRect.height()) {
            runnable();
        }
    }
}

这样使用:

myView.onSizeChange { 
     // Do you thing...
}

答案 4 :(得分:0)

感谢https://stackoverflow.com/users/2402790/michael-allan,如果您不想覆盖所有视图,这是一种好方法。 随着Api的发展,我建议改为粘贴此副本:

    String TAG="toto";
    ///and last listen for size changed
    YourView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v,
                                   int left, int top, int right, int bottom,
                                   int oldLeft, int oldTop, int oldRight, int oldBottom) {

           boolean widthChanged=((right-left)!=(oldRight-oldLeft));
            if( widthChanged )
            {
                // width has changed
                Log.e(TAG,"Width has changed new width is "+(right-left)+"px");
            }
            boolean heightChanged=((bottom-top)!=(oldBottom-oldTop));
            if( heightChanged)
            {
                // height has changed
                Log.e(TAG,"height has changed new height is "+(bottom-top)+"px");
            }
        }
    });