当CollapsingToolbarLayout打开时,我尝试隐藏菜单项。
我这样做:XML
<android.support.design.widget.CoordinatorLayout
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" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context="com.compafone.app.ViewNews">
<android.support.design.widget.AppBarLayout android:id="@+id/app_bar"
android:fitsSystemWindows="true" android:layout_height="@dimen/app_bar_height"
android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout android:id="@+id/toolbar_layout"
android:fitsSystemWindows="true" android:layout_width="match_parent"
android:scaleType="centerCrop" android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:layout_height="match_parent" app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="?attr/colorPrimary">
<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize" android:layout_width="match_parent"
app:layout_collapseMode="pin" app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_view_news" />
<android.support.design.widget.FloatingActionButton android:id="@+id/fab"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin" app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end" android:src="@drawable/chatboxes" />
这是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="com.compafone.app.ViewNews">
<item
android:id="@+id/action_comments"
android:title="Comments"
android:orderInCategory="200"
android:icon="@drawable/chatboxes"
app:showAsAction="ifRoom" />
<item
android:id="@+id/menu_item_share"
android:title="Partager"
android:icon="@android:drawable/ic_menu_share"
android:orderInCategory="100"
app:showAsAction="ifRoom"/>
然后,这是Java活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_news);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.arrowleft);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
//On récupère l'intent
Intent i = getIntent();
id = i.getStringExtra("ID");
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
collapsingToolbarLayout.setExpandedTitleColor(getResources().getColor(android.R.color.transparent));
collapsingToolbarLayout.setTitle("Compafone");
appbar = (AppBarLayout) findViewById(R.id.app_bar);
new NewsListActivityLoad().execute();
title = (TextView) findViewById(R.id.news_title);
content = (TextView) findViewById(R.id.news_content);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu resource
getMenuInflater().inflate(R.menu.menu_view_news, menu);
MenuItem menuItem = menu.findItem(R.id.menu_item_share);
//mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; goto parent activity.
this.finish();
this.overridePendingTransition(0, R.anim.abc_fade_out);
return true;
case R.id.action_comments:
Intent intent = getIntent();
//Intent intent3 = new Intent(this, comments.class);
try {
//intent3.putExtra(Intent_ID, intent.getStringExtra(EXTRA_PROJET));
//intent3.putExtra(Intent_SUCCES, "null");
//startActivity(intent3);
} catch (Exception e) {
Log.i("notFoundProject", "Le projet n'existe pas");
}
return true;
case R.id.menu_item_share:
//doShare(shareintent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
因此,我希望仅在用户向下滚动时隐藏菜单中的项目并显示项目(当工具栏处于正常大小时,因此在工具栏中显示工具栏标题时同时显示项目。)
抱歉英语不好,提前谢谢。