How to pin two buttons below toolbar while scrolling

后端 未结 2 1538
没有蜡笔的小新
没有蜡笔的小新 2020-12-24 14:34

I want to achieve something like this as shown in image

I have tried this code but not able to pin button below top image or toolbar

In this I am tr

2条回答
  •  庸人自扰
    2020-12-24 15:28

    I would suggest taking advantage of the CoordinatorLayout by putting the content above the tabs inside a CollapsingToolbarLayout. And using a TabLayout instead of two Buttons, which should be more in line with the Android design guidelines (though you could just swap the TabLayout for a LinearLayout with two buttons if your prefer):

    
    
        
    
            
    
                
    
                    
    
                
    
                
    
            
    
            
    
        
    
        
    
            
    
        
    
    
    

    Notice that the TabLayout is outside of the CollapsingToolbarLayout, but still inside of the AppBarLayout. This will keep the TabLayout pinned underneath the Toolbar and whatever content you put inside the CollapsingToolbarLayout. And no need for multiple NestedScrollView layouts.

    There may be some problems with the spacing of the LinearLayout (some content may be hidden under the Toolbar), but if that is the case, you should be able to set the top margin of the LinearLayout to ?attr/actionBarSize. I haven't tested any of this, so comment if you run into issues.


    You can even swap out the NestedScrollView for a ViewPager (make sure you keep the app:layout_behavior though). This would allow you to easily tie the tabs to two fragments using mTabLayout.setupWithViewPager(mViewPager).


    EDIT After testing, unfortunately this does not seem to work the way I had hoped. The problem is that part of the content is hidden underneath the Toolbar, even if you set the top margin to ?attr/actionBarSize. This is because, for some reason, the height of the status bar is not included in the top margin of the layout. You could try to guess what the top margin should be, but this can lead to unexpected padding between the toolbar and the content.

提交回复
热议问题