Overlaying content above AppBarLayout using new Material Design

匿名 (未验证) 提交于 2019-12-03 01:23:02

问题:

I want to achieve something like that. (not the FAB or the Snackbar). How can i create a layout, overlaying the AppBarLayout? Like this! (For Example)

Like Play Store:

My AppBarLayout with CoordinatorLayout and NestedScrollView with RelativeLayout as content looks like this:

It would be awesome if someone could help me out. I can not find anything on the internet...

Thanks in advance!

回答1:

Just add something like

app:behavior_overlapTop="64dp" 

to your NestedScrollView and it will be placed above the expanded toolbar.

In addition, you should add something like

app:expandedTitleMarginBottom="70dp" 

to your CollapsingToolbarLayout so the title does not appear under your overlaid scroll content.



回答2:

It's quite simple, really. You could achieve that by using a combination of ToolBar, FrameLayout, and your content view (could be a ListView like your first example, or anything).

The idea is to make your FrameLayout possess the same color as your ToolBar, giving the illusion of ToolBar being much larger than it is. Then all that is left to do is to make your content view be the last (or in API 21 and above: possess the highest elevation attribute) so that it would appear as if it floats above the aforementioned FrameLayout.

See my illustration below:

Now that you got the big idea, below is some real live XML snippet for doing such thing. (I actually use this layout in one of my apps) :

  ....        .... 

Note that my ly_content has higher elevation value than that of v_toolbar_extension. This is what will give you that desired 'overlaid toolbar' effect.

Last but not least, you would want to add this line somewhere in your activity's onCreate() :

/* Assuming mToolbar exists as a reference to your ToolBar in XML. */ setSupportActionBar(mTbToolbar); getSupportActionBar().setElevation(0); 

What that codes woud do is to set your ToolBar elevation to zero; removing preset shadows that were given as a default to ToolBars. If you don't do this, said shadow will create a "seam" between your ToolBar and your FrameLayout, thus breaking the illusion of those two being the same.

p.s., It is also important to give your content view a padding on each side. Doing so, your content view will not cover the entire width of the screen (which would render this effect useless).


Note: I see some good answers here that mentioned the absence of FrameLayout and instead making the ToolBar taller. While in theory it might work as well as my proposed solution, you might have problems when trying to manipulate scrolling; by doing that, you won't be able to separate ToolBar and its extension. You'll be forced to either make the Toolbar static or scroll all of the ToolBar altogether (makes scrolling a bit weird).

Add to that, the fact that you can't easily assign a custom drawable into a Toolbar. Hence makes it hard to follow the Google Play example you've given above. While if you're using my solution, all you'd need to do is just make your Toolbar transparent and assign the drawable to the FrameLayout instead.



回答3:

I did try to implement effects like you referred which is called Card Toolbar in Android, and it did work as expected. Here is my layout, Take a look at it:

Hope you'll be inspired.



回答4:

I had a similar requirement and I achieved it as below.

Your activity theme should extend Theme.AppCompat.Light.NoActionBar. I created a Layout XML File as:

And the Activity should be something like this:

public class MainActivity extends ActionBarActivity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          Toolbar maintoolbar = (Toolbar) findViewById(R.id.toolbar_main);         setSupportActionBar(maintoolbar);         getSupportActionBar().setDisplayHomeAsUpEnabled(true);     } } 

I got a view like this :



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!