Android CollapsingToolbarLayout:将ActionBar载入Toolbar(2)
我之前写了一篇文章:《Android Material Design : CollapsingToolbarLayout使用简介》文章链接地址:http://blog.csdn.net/zhangphil/article/details/48930821
这篇文章主要介绍了Android CollapsingToolbarLayout使用方法。在此篇文章的基础上,增加一点儿内容,就是将Android ActionBar装载到Toolbar里面。
测试的activity MainActivity.java :
package zhangphil.demo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
注意在AndroidManifest定义MainActivity的theme时写上:
android:theme="@style/Theme.AppCompat.Light.NoActionBar"这段代码写上,才可以使得Toolbar取代ActionBar。
MainActivity.java需要的布局文件activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="300dip" android:background="#1e88e5" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" > <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/collapsing_toolbar" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_scrollFlags="scroll|exitUntilCollapsed" app:contentScrim="#ff5252" app:expandedTitleMarginEnd="60dp" app:expandedTitleMarginStart="50dp" > <ImageView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_collapseMode="parallax" android:background="@drawable/ic_launcher" android:scaleType="centerCrop" /> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="50dp" android:text="0" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="50dp" android:text="1" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="50dp" android:text="2" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="50dp" android:text="3" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="50dp" android:text="4" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="50dp" android:text="5" /> </LinearLayout> </android.support.v4.widget.NestedScrollView> <android.support.design.widget.FloatingActionButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dip" app:layout_anchor="@+id/appbar" app:layout_anchorGravity="bottom|right|end" android:clickable="true" android:src="@drawable/ic_launcher" /> </android.support.design.widget.CoordinatorLayout>
同时MainActivity.java需要的res/menu/main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="zhangphil.demo.MainActivity" > <item android:id="@+id/action_settings" android:orderInCategory="100" android:title="设置" app:showAsAction="never"/> </menu>
代码运行结果: