我有折叠工具栏和片段中的选项菜单。当我折叠工具栏时,菜单移动到状态栏上方。崩溃后,我没有看到 菜单选项。我在截图中显示:
这是我的.xml代码:
<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/abl_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/ctl_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|snap|exitUntilCollapsed"
app:contentScrim="?attr/colorPrimary">
<ImageView
android:id="@+id/iv_avatar"
android:layout_width="match_parent"
android:layout_height="220dp"
android:src="@drawable/ic_main_logo"
android:scaleType="centerInside"
android:background="@drawable/shape_profile_avatar_background"
app:layout_collapseMode="parallax"/>
<include layout="@layout/component_transparent_toolbar"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_results"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/btn_avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:elevation="4dp"
app:elevation="4dp"
app:srcCompat="@drawable/vector_photo_camera_black_24dp"
app:layout_anchor="@id/abl_toolbar"
app:layout_anchorGravity="bottom|right|end"/>
<include layout="@layout/component_profile_avatar_bottom_sheet" />
这里是我的代码片段:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mActivity = (MainActivity) getActivity();
mAdapter = new ProfileRVAdapter();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile, parent, false);
mIVAvatar = (ImageView) view.findViewById(R.id.iv_avatar);
LinearLayoutManager manager = new LinearLayoutManager(mActivity);
RecyclerView rvResults = (RecyclerView) view.findViewById(R.id.rv_results);
rvResults.setLayoutManager(manager);
rvResults.setAdapter(mAdapter);
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
toolbar.setTitle(R.string.activity_main_profile_label);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
toolbar.setPadding(0, mActivity.getStatusBarHeight(), 0, 0);
toolbar.getLayoutParams().height = toolbar.getLayoutParams().height + mActivity.getStatusBarHeight();
}
mActivity.setSupportActionBar(toolbar);
view.findViewById(R.id.btn_avatar).setOnClickListener(this);
view.findViewById(R.id.btn_cancel).setOnClickListener(this);
view.findViewById(R.id.btn_gallery).setOnClickListener(this);
view.findViewById(R.id.btn_photo).setOnClickListener(this);
mBottomSheet = BottomSheetBehavior.from(view.findViewById(R.id.ll_bottom_sheet));
mBottomSheet.setState(BottomSheetBehavior.STATE_HIDDEN);
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_profile, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_edit:
return true;
case R.id.menu_logout:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
知道为什么会这样吗?
已编辑/已解决: 我解决了我的问题。错误在工具栏属性中,我忘了app:layout_collapseMode =“pin”。
答案 0 :(得分:0)
在Toolbar
文件中设置layout.xml
代码中Activity's layout
的设计代码&amp;
在Activity
代码
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//在活动文件中添加此行代码。