为什么搜索按钮没有显示在导航器上

时间:2018-02-24 08:02:31

标签: android

您好我正在尝试在片段上放置一个搜索按钮,但我不知道怎么做,或者由于搜索按钮根本没有显示该怎么做。在Appbar上

导航栏效果很好,一切都很好

我的管理员活动

public class AdminActivity extends AppCompatActivity {
private DrawerLayout mdrawerl;
private ActionBarDrawerToggle mtoggle;
private FirebaseAuth auth;
private com.google.firebase.ikuzou.database.MemberDetailAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_admin);
    auth = FirebaseAuth.getInstance();
    mdrawerl = (DrawerLayout) findViewById(R.id.drawer);
    mtoggle = new ActionBarDrawerToggle(this,mdrawerl,R.string.open, R.string.close);
    mdrawerl.addDrawerListener(mtoggle);
    NavigationView nvDrawer = (NavigationView) findViewById(R.id.nv) ;
    mtoggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    setupDrawerContent(nvDrawer);
    memberFragment frag = new memberFragment();
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.flcontent, frag)
            .commit();

}


@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (mtoggle.onOptionsItemSelected(item)){
        return true;
    }
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    // getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


public void selectItemDrawer(MenuItem menuItem) {
    auth = FirebaseAuth.getInstance();
    Fragment myFragment = null;
    Class fragmentClass;
    switch (menuItem.getItemId()){
        case R.id.signout:
            auth.getInstance().signOut();
            Intent i = new Intent(AdminActivity.this, LoginActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                    Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(i);
            return;

    }

    switch (menuItem.getItemId()){
        case R.id.members:
            fragmentClass = memberFragment.class;
            break;
        case R.id.transactions:
            fragmentClass = transaction.class;
            break;
        default:
            fragmentClass = memberFragment.class;
    }
    try {
        myFragment = (Fragment) fragmentClass.newInstance();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.flcontent,myFragment).commit();
    menuItem.setChecked(true);
    setTitle(menuItem.getTitle());
    mdrawerl.closeDrawers();

}

private void setupDrawerContent(NavigationView navigationView) {
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            selectItemDrawer(item);
            return false;
        }
    });
}

这是我想在appbar上放置搜索视图按钮的片段

我的片段活动

  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_member2, container, false);
    mAdapter = new com.google.firebase.ikuzou.database.MemberDetailAdapter(this);
    RecyclerView view1 = (RecyclerView) view.findViewById(R.id.recycler_view);
    view1.setLayoutManager(new LinearLayoutManager(getContext()));
    view1.setHasFixedSize(true);
    view1.setAdapter(mAdapter);
    return view;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.search_menu, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) searchItem.getActionView();
    searchView.setOnQueryTextListener(this);
    super.onCreateOptionsMenu(menu, inflater);

    super.onCreateOptionsMenu(menu, inflater);
}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@drawable/androidicon"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
    <activity android:name=".LoginActivity">
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
        <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".signup" />
    <activity android:name=".ResetPasswordActivity" />
    <activity android:name=".AdminActivity" />
    <activity android:name=".UserActivity"></activity>
</application>

我的菜单资源文件夹

enter image description here

1 个答案:

答案 0 :(得分:1)

只需在你的frage中调用此方法onCreate()setHasOptionMenu(true)