- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带 fragment 的抽屉导航。在所有 fragment 中,它在 main.xml
中显示一个按钮项。
现在,我想在一些 fragment 中实现搜索 View 。为此,我在菜单文件夹中制作了 search.xml
。
search.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/search"
android:title="@string/search_hint"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
</menu>
我需要执行哪些操作才能实现这一目标?
提前致谢
更新:主要 Activity
public class MainActivity extends ActionBarActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
// slide menu items
private String[] navMenuTitles;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SpannableString s = new SpannableString(getTitle());
s.setSpan(new TypefaceSpan(this, "BOOKOS.TTF"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Update the action bar title with the TypefaceSpan instance
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle(s);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItems = new ArrayList<NavDrawerItem>();
// adding nav drawer items to array
// Home
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], 1));
// Find People
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1],1));
// Photos
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], 1));
// Communities, Will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], 1));
// Pages
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], 1));
// What's hot, We will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], 1));
// Recycle the typed array
// navMenuIcons.recycle();
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
// enabling action bar app icon and behaving it as toggle button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
){
@SuppressLint("NewApi")
public void onDrawerClosed(View view) {
SpannableString s = new SpannableString(mTitle);
s.setSpan(new TypefaceSpan(getApplicationContext(), "BOOKOS.TTF"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Update the action bar title with the TypefaceSpan instance
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle(s);
// getSupportActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
supportInvalidateOptionsMenu();
}
@SuppressLint("NewApi")
public void onDrawerOpened(View drawerView) {
SpannableString s = new SpannableString(mDrawerTitle);
s.setSpan(new TypefaceSpan(getApplicationContext(), "BOOKOS.TTF"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Update the action bar title with the TypefaceSpan instance
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle(s);
// getSupportActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
supportInvalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for first nav item
// displayView(0);
}
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
displayView(7);
}
/**
* Slide menu item click listener
* */
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}
/**
* Diplaying fragment view for selected nav drawer list item
* */
@SuppressLint("NewApi")
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new IntroductionFragment();
break;
case 1:
fragment = new PrefaceFragment();
break;
case 2:
fragment = new PreambleFragment();
break;
case 3:
fragment = new ContentsFragment();
break;
case 4:
fragment = new SchedulesFragment();
break;
case 5:
fragment = new AppendixFragment();
break;
case 6:
fragment = new AmendmentFragment();
break;
case 7:
fragment = new PhotoFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
ActionProvider mActionProvider;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
// Find the share item
MenuItem shareItem = menu.findItem(R.id.action_share);
// Need to use MenuItemCompat to retrieve the Action Provider
mActionProvider = (ActionProvider)
MenuItemCompat.getActionProvider(shareItem);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_share:
Intent ints=new Intent(this,ShareActivity.class);
startActivity(ints);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/***
* Called when invalidateOptionsMenu() is triggered
*/
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_share).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@SuppressLint("NewApi")
@Override
public void setTitle(CharSequence title) {
SpannableString s = new SpannableString(mTitle);
s.setSpan(new TypefaceSpan(this, "BOOKOS.TTF"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mTitle = title;
getSupportActionBar().setTitle(s);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
我的 fragment 类
public class IntroductionFragment extends Fragment{
public IntroductionFragment(){
setRetainInstance(true);
this.setHasOptionsMenu(true);
}
Button b;
TextView tv1;
@SuppressLint("NewApi")
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_introduction,container,false);
SpannableString s = new SpannableString("Introduction");
s.setSpan(new TypefaceSpan(getActivity(), "BOOKOS.TTF"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(), "BOOKOS.TTF");
tv1 = (TextView)rootView.findViewById(R.id.textView1);
tv1.setTypeface(typeface);
getActivity().supportInvalidateOptionsMenu();
return rootView;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.v("inside onCreateOptionsMenu","???????");
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
getActivity().getMenuInflater().inflate(R.menu.search, menu);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.fragment_introduction, null);
ViewGroup rootView = (ViewGroup) getView();
rootView.removeAllViews();
rootView.addView(view);
}
}
这是日志记录
07-27 17:27:16.200: V/inside onCreateOptionsMenu(19875): ???????
07-27 17:27:16.210: W/dalvikvm(19875): threadid=1: thread exiting with uncaught exception (group=0x40d98450)
07-27 17:27:16.280: E/AndroidRuntime(19875): FATAL EXCEPTION: main
07-27 17:27:16.280: E/AndroidRuntime(19875): java.lang.NullPointerException
07-27 17:27:16.280: E/AndroidRuntime(19875): at com.vivekwarde.indianconstitution.MainActivity.onPrepareOptionsMenu(MainActivity.java:244)
07-27 17:27:16.280: E/AndroidRuntime(19875): at android.app.Activity.onPreparePanel(Activity.java:2494)
07-27 17:27:16.280: E/AndroidRuntime(19875): at android.support.v4.app.FragmentActivity.onPrepareOptionsPanel(FragmentActivity.java:491)
07-27 17:27:16.280: E/AndroidRuntime(19875): at android.support.v7.app.ActionBarActivity.superOnPrepareOptionsPanel(ActionBarActivity.java:240)
07-27 17:27:16.280: E/AndroidRuntime(19875): at android.support.v7.app.ActionBarActivityDelegate.onPrepareOptionsPanel(ActionBarActivityDelegate.java:160)
07-27 17:27:16.280: E/AndroidRuntime(19875): at android.support.v7.app.ActionBarActivity.onPrepareOptionsPanel(ActionBarActivity.java:212)
07-27 17:27:16.280: E/AndroidRuntime(19875): at android.support.v4.app.FragmentActivity.onPreparePanel(FragmentActivity.java:480)
07-27 17:27:16.280: E/AndroidRuntime(19875): at android.support.v7.app.ActionBarActivity.superOnPreparePanel(ActionBarActivity.java:236)
07-27 17:27:16.280: E/AndroidRuntime(19875): at android.support.v7.app.ActionBarActivityDelegateICS.onPreparePanel(ActionBarActivityDelegateICS.java:155)
07-27 17:27:16.280: E/AndroidRuntime(19875): at android.support.v7.app.ActionBarActivity.onPreparePanel(ActionBarActivity.java:204)
07-27 17:27:16.280: E/AndroidRuntime(19875): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onPreparePanel(ActionBarActivityDelegateICS.java:290)
07-27 17:27:16.280: E/AndroidRuntime(19875): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:425)
07-27 17:27:16.280: E/AndroidRuntime(19875): at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:751)
07-27 17:27:16.280: E/AndroidRuntime(19875): at android.app.Activity.invalidateOptionsMenu(Activity.java:2595)
07-27 17:27:16.280: E/AndroidRuntime(19875): at android.support.v4.app.ActivityCompatHoneycomb.invalidateOptionsMenu(ActivityCompatHoneycomb.java:29)
07-27 17:27:16.280: E/AndroidRuntime(19875): at android.support.v4.app.FragmentActivity.supportInvalidateOptionsMenu(FragmentActivity.java:648)
07-27 17:27:16.280: E/AndroidRuntime(19875): at android.support.v7.app.ActionBarActivity.supportInvalidateOptionsMenu(ActionBarActivity.java:170)
07-27 17:27:16.280: E/AndroidRuntime(19875): at com.vivekwarde.indianconstitution.IntroductionFragment.onCreateView(IntroductionFragment.java:48)
07-27 17:27:16.280: E/AndroidRuntime(19875): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
07-27 17:27:16.280: E/AndroidRuntime(19875): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
07-27 17:27:16.280: E/AndroidRuntime(19875): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
07-27 17:27:16.280: E/AndroidRuntime(19875): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-27 17:27:16.280: E/AndroidRuntime(19875): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
07-27 17:27:16.280: E/AndroidRuntime(19875): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
07-27 17:27:16.280: E/AndroidRuntime(19875): at android.os.Handler.handleCallback(Handler.java:615)
07-27 17:27:16.280: E/AndroidRuntime(19875): at android.os.Handler.dispatchMessage(Handler.java:92)
07-27 17:27:16.280: E/AndroidRuntime(19875): at android.os.Looper.loop(Looper.java:137)
07-27 17:27:16.280: E/AndroidRuntime(19875): at android.app.ActivityThread.main(ActivityThread.java:4794)
07-27 17:27:16.280: E/AndroidRuntime(19875): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 17:27:16.280: E/AndroidRuntime(19875): at java.lang.reflect.Method.invoke(Method.java:511)
07-27 17:27:16.280: E/AndroidRuntime(19875): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-27 17:27:16.280: E/AndroidRuntime(19875): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-27 17:27:16.280: E/AndroidRuntime(19875): at dalvik.system.NativeStart.main(Native Method)
最佳答案
对于每个需要有菜单的 fragment :
this.setHasOptionsMenu(true)
。onCreateOptionsMenu(..)
和 onOptionsItemSelected(..)
来处理该 fragment 的菜单项。在抽屉布局中:
invalidateOptionsMenu()
。如果您使用的是 support-v4 fragment ,请调用 supportInvalidateOptionsMenu()
。 Android 版本在处理这些东西的方式上存在一些差异,因此请确保分别在 Ice Cream Sandwich、Jelly Bean 和 KitKat 上进行测试。
关于android - 在 Navigation Drawer 的每个 fragment 中实现不同的 Action Bar 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24978984/
我是 React Native 的新手,目前正在研究 React Native Navigation Docs 。我想知道:navigation.push() 和 navigation.navigat
来自 this和 this , 我了解到 navigation.navigate() 之间存在有意义的差异和 navigation.push() .尽管如此,我仍然想知道我是否可以使用 navigat
我正在使用 react-navigation 3.6.1 和 react-native-camera 2.2.2。当我拍照时,进入下一个屏幕的按钮在第一次按下时没有任何作用。必须按多次 这是针对 io
androidx.navigation:navigation-ui 和 android.arch.navigation:navigation-ui-ktx 有什么区别? 当我在我的 gradle 中使
使用 TypeScript 时,react-navigation 中发生的一个常见错误是: Argument of type 'string' is not assignable to paramet
我有一个使用 React Native 的应用程序,我正在使用 react-navigation (5.2.9)。 我构建了一个 Stack.Navigator,我有我的屏幕,但我希望页脚组件在外面,
我有一个组件在 NavigationContainer 组件内但在 Stack Navigator 之外。我能知道组件中的当前路由是什么吗?我不能使用 useRoute Hook ,因为它出错了: C
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 3年前关闭。 Improve this questi
我正在学习 react-native 导航 https://reactnavigation.org/docs/intro/ 。我在那里的例子中看到 class HomeScreen extends R
我在我的 native 项目中使用 eslint airbnb。如果我没有验证 Prop ,特别是来自 react-navigation 的 Prop ,eslint 会抛出错误 linting .
在我的应用程序中,我有 6 个不同的页面。 有 1 个页面称为“收藏夹页面”,其中列出了用户选择的收藏夹。它们由一列显示,该列使用来自另一个类的列表。列表代码如下: List favoriteCo
我在 react 导航问题部分看到很多人使用 this.props.navigation.dispatch 以编程方式导航。是否有任何特定原因或用例需要在 this.props.navigation.
我在 vue.js 中开始了一个新元素。我添加了导航栏。有一次,我注意到控制台中有问题: Audit usage of navigator.userAgent, navigator.appVersio
首先,是的,我知道有很多与此问题相关的答案,但我发现了这种在 react-native 0.60 中进行导航的特定方法,问题是,我还没有弄清楚,而且我不想刮掉这个导航方法来测试不同的方法,这是导航文件
react 导航 5 我在 TabNavigator 内部构建了一个 StackNavigator,并且主屏幕和其他屏幕之间的导航正在工作。但问题是,当我从 Tab2 移动到 Tab1 时,我希望 T
我正在尝试在 react-native with expo 中使用 react-navigation-redux-helpers 将 redux 与 react-navigation 集成。但是,尽管
我正在尝试使用最新的 react-navigation 版本运行我的应用程序但无法正常工作。也尝试使用此解决方案 ( Invariant Violation: The navigation prop
我正在尝试使用 React Navigation 和 StackNavigator 在我的应用中导航。 我有一个带有 onPress={() => navigate('DetailsScreen'
我在我的应用程序中使用 react 导航。如果我从 screen A 导航到 Screen B,如果转换完成,我如何获得一些反馈。 最佳答案 从 'react-native 导入 { Interact
我遵循的是身份验证流程 (https://reactnavigation.org/docs/en/auth-flow.html) 上的 React 导航指南,但略有不同。我在 LoginScreen
我是一名优秀的程序员,十分优秀!