如何根据底部导航栏菜单设置工具栏菜单

时间:2017-11-13 13:06:30

标签: android

我是android的新手,我从6个月开始学习。目前我正在开发一个项目,我想为不同的底部导航栏项添加不同的工具栏菜单,我正在使用片段作为项目。

我的主要活动布局代码是:

<LinearLayout 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:orientation="vertical">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbarHome"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbarHome"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize" />

</android.support.design.widget.AppBarLayout>


<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/menu_bottom_navigation" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/btnfab_Add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:layout_marginBottom="@dimen/size_20dp"
        android:clickable="true"
        android:focusable="true"
        app:elevation="8dp"
        app:fabSize="normal"
        app:srcCompat="@android:drawable/ic_input_add" />

</FrameLayout>

和我的mainactivity java代码是这样的:

public class Home extends AppCompatActivity {

private  SharedPreferences sharedPreferences;

//xml references
BottomNavigationView navigation;


private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        switch (item.getItemId()) {
            case R.id.action_home:
                transaction.replace(R.id.content, new HomeFragment()).commit();
                return true;
            case R.id.action_notification:
                transaction.replace(R.id.content, new ActivityLogFragment()).commit();
                return true;
        }

        return false;
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    sharedPreferences = getSharedPreferences(HomePrev.AccountType, MODE_PRIVATE);

    // create class object
    GPSTracker gps = new GPSTracker(this);

    // check if GPS enabled
    if (gps.canGetLocation()) {

        String latitude = String.valueOf(gps.getLatitude());
        String longitude = String.valueOf(gps.getLongitude());

        SharedPreferences.Editor editor = sharedPreferences.edit();

        editor.putString("Lattitude", latitude);
        editor.putString("Longitude", longitude);
        editor.apply();

    } else {
        // can't get location
        // GPS or Network is not enabled
        // Ask user to enable GPS/network in settings
        gps.showSettingsAlert();
    }

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarHome);
    setSupportActionBar(toolbar);
   navigation = findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    //Manually displaying the first fragment - one time only
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.content, new HomeFragment());
    transaction.commit();

    FloatingActionButton btnFabAdd = findViewById(R.id.btnfab_Add);


}

@Override
public void onBackPressed() {
    new AlertDialog.Builder(this)
            .setTitle("Really Exit?")
            .setMessage("Are you sure you want to exit?")
            .setNegativeButton(android.R.string.no, null)
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface arg0, int arg1) {
                    Home.super.onBackPressed();
                }
            }).create().show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.


    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.home, menu);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the HomePrev/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

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

        startActivity(new Intent(Home.this,Notifications.class));
        return true;

    }
    else if (id == R.id.action_search) {
        return true;
    } else if (id == R.id.action_profile_info) {

        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

所以我的问题是如何根据用户选择的底部导航栏项添加不同的菜单。

请尽快恢复,如果您无法理解我的问题,请回复我,但不要暂停此问题。

0 个答案:

没有答案