CoordinatorLayout ignores margins for views with anchor

前端 未结 6 2146
春和景丽
春和景丽 2021-02-12 03:49

Given I\'m using a layout like this:



        
6条回答
  •  一生所求
    2021-02-12 04:41

    To anchor the FloatingActionButton below the AppBar like this:

    Extend the FloatingActionButton and override offsetTopAndBottom:

    public class OffsetFloatingActionButton extends FloatingActionButton
    {
        public OffsetFloatingActionButton(Context context)
        {
            this(context, null);
        }
    
        public OffsetFloatingActionButton(Context context, AttributeSet attrs)
        {
            this(context, attrs, 0);
        }
    
        public OffsetFloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr)
        {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        protected void onLayout(boolean changed, int left, int top, int right, int bottom)
        {
            super.onLayout(changed, left, top, right, bottom);
            ViewCompat.offsetTopAndBottom(this, 0);
        }
    
        @Override
        public void offsetTopAndBottom(int offset)
        {
            super.offsetTopAndBottom((int) (offset + (getHeight() * 0.5f)));
        }
    }
    

提交回复
热议问题