我已经深入到正在崩溃的工具栏布局,并在谷歌上搜索它,但我认为这是不可能的。因此,为了解决这个问题,我创建了一个带有菜单溢出图标的ImageView,并在其下方显示弹出列表,以模拟操作菜单行为。
下面是我的折叠工具栏布局在XML中的样子
<android.support.design.widget.CollapsingToolbarLayout
android:fitsSystemWindows="false"
android:id="@+id/collapsing_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:contentInsetStartWithNavigation="0dp"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="exitUntilCollapsed|scroll">
<your.app.AutoFitImageView
android:fitsSystemWindows="false"
android:id="@+id/iv_backdrop_image"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:scaleType="fitCenter"
android:transitionName="image"
app:layout_collapseMode="parallax"
tools:targetApi="lollipop"/>
<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/ThemeOverlay.AppCompat.Light">
<ImageView
android:id="@+id/button_menu"
android:layout_gravity="right"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:minHeight="48dp"
android:minWidth="48dp"
android:scaleType="centerInside"
android:src="@drawable/ic_action_overflow"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
然后,我使用将clickListener设置为ImageView
R.id.button_menu
调用此方法的id:
private void showListMenu(View anchor) {
final ListPopupWindow popupWindow = new ListPopupWindow(getActivity());
List<HashMap<String, String>> data = new ArrayList<>();
HashMap<String, String> map1 = new HashMap<>();
map1.put("menu", "Share Detail");
HashMap<String, String> map2 = new HashMap<>();
map2.put("menu", "Share First Trailer");
data.add(map1);
data.add(map2);
final Movie movieData = getMovieData();
HashMap<String, String> map3 = new HashMap<>();
if (DbHelper.movieInDb(movieData)) {
map3.put("menu", getString(R.string.unfavorite));
} else {
map3.put("menu", getString(R.string.favorite));
}
data.add(map3);
ListAdapter adapter = new SimpleAdapter(
getActivity(),
data,
android.R.layout.simple_list_item_1, // You may want to use your own cool layout
new String[]{"menu"}, // These are just the keys that the data uses
new int[]{android.R.id.text1}); // The view ids to map the data to
popupWindow.setAnchorView(anchor);
popupWindow.setAdapter(adapter);
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
popupWindow.setWidth(displaymetrics.widthPixels / 3); // note: don't use pixels, use a dimen resource
popupWindow.setOnItemClickListener(yourListener); // the callback for when a list item is selected
popupWindow.show();
}
单击时,该方法将显示ListPopUpWindow,其样式与溢出操作菜单相同。