Determining the size of an Android view at runtime

前端 未结 12 2008
陌清茗
陌清茗 2020-11-22 09:59

I am trying to apply an animation to a view in my Android app after my activity is created. To do this, I need to determine the current size of the view, and then set up an

12条回答
  •  半阙折子戏
    2020-11-22 10:13

    Here is the code for getting the layout via overriding a view if API < 11 (API 11 includes the View.OnLayoutChangedListener feature):

    public class CustomListView extends ListView
    {
        private OnLayoutChangedListener layoutChangedListener;
    
        public CustomListView(Context context)
        {
            super(context);
        }
    
        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b)
        {
            if (layoutChangedListener != null)
            {
                layoutChangedListener.onLayout(changed, l, t, r, b);
            }
            super.onLayout(changed, l, t, r, b);
        }
    
        public void setLayoutChangedListener(
            OnLayoutChangedListener layoutChangedListener)
        {
            this.layoutChangedListener = layoutChangedListener;
        }
    }
    public interface OnLayoutChangedListener
    {
        void onLayout(boolean changed, int l, int t, int r, int b);
    }
    

提交回复
热议问题