如何检测布局调整大小?

时间:2022-01-09 04:53:16

Dianne Hackborn mentioned in a couple threads that you can detect when a layout as been resized, for example, when the soft keyboard opens or closes. Such a thread is this one... http://groups.google.com/group/android-developers/browse_thread/thread/d318901586313204/2b2c2c7d4bb04e1b

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

However, I didn't understand her answer: "By your view hierarchy being resized with all of the corresponding layout traversal and callbacks."

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

Does anyone have a further description or some examples of how to detect this? Which callbacks can I link into in order to detect this?

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

Thanks

3 个解决方案

#1


44  

Override onSizeChanged in your View!

在视图中覆盖onSizeChanged!

#2


25  

One way is View.addOnLayoutChangeListener. There's no need to subclass the view in this case. But you do need API level 11. And the correct calculation of size from bounds (undocumented in the API) can sometimes be a pitfall. Here's a correct example:

一种方法是View.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
    }
}

Another way (as dacwe answers) is to subclass your view and override onSizeChanged.

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

#3


3  

My solution is to add an invisible tiny dumb view at the end of of the layout / fragment (or add it as a background), thus any change on size of the layout will trigger the layout change event for that view which could be catched up by OnLayoutChangeListener:

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

Example of adding the dumb view to the end of the layout:

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

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

Listen the event:

听取这个事件:

    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
        }
    });

#1


44  

Override onSizeChanged in your View!

在视图中覆盖onSizeChanged!

#2


25  

One way is View.addOnLayoutChangeListener. There's no need to subclass the view in this case. But you do need API level 11. And the correct calculation of size from bounds (undocumented in the API) can sometimes be a pitfall. Here's a correct example:

一种方法是View.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
    }
}

Another way (as dacwe answers) is to subclass your view and override onSizeChanged.

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

#3


3  

My solution is to add an invisible tiny dumb view at the end of of the layout / fragment (or add it as a background), thus any change on size of the layout will trigger the layout change event for that view which could be catched up by OnLayoutChangeListener:

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

Example of adding the dumb view to the end of the layout:

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

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

Listen the event:

听取这个事件:

    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
        }
    });