如何在片段页面上方创建导航栏

时间:2017-03-22 21:18:50

标签: android android-layout android-fragments uinavigationbar

我正在尝试制作一些简单的应用程序,您可以在片段类之间切换,但导航菜单会粘在顶部。我是Android开发的新手,我们将不胜感激。 这是我的主要活动     import android.support.v4.app.Fragment;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

     DrawerLayout drawer = (DrawerLayout)         
findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open,         
R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView)    
findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@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;
}

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

    //noinspection SimplifiableIfStatement

    return super.onOptionsItemSelected(item);
}



private void displaySelectedScreen(int id)
{
    Fragment fragment = null;
    switch(id){
        case R.id.syllabus:
            fragment = new Syllabus();
            break;
        case R.id.questionss:
            fragment = new Questions();
            break;
        case R.id.definitions:
            fragment = new Definitions();
            break;
        case R.id.contact_us:
            fragment = new Contact_Us();
            break;
    }

    if(fragment != null){

        FragmentTransaction ft = 
getSupportFragmentManager().beginTransaction();
        ft.replace(android.R.id.content,fragment);
        ft.commit();
    }
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);


}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();



    displaySelectedScreen(id);
    return true;
}
}

我的主要活动XML是这样的:

<?xml version="1.0" encoding="utf-8"?>   
<android.support.v4.widget.DrawerLayout      
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />


<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

内容主要是:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout     
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.weebly.dunyaibdp.chemistryforib.MainActivity"
    tools:showIn="@layout/app_bar_main"
    android:fitsSystemWindows="true">
</android.support.constraint.ConstraintLayout>

我的所有片段类都是这样的:

public class Questions extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup    
container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.questions,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle   
savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getActivity().setTitle("questions");
    }
}

现在,当我运行我的应用程序时,一切都很好。我点击片段之间的导航切换,但当我在片段中点击导航栏时,导航栏位于片段下方。我的意思是我能够切换,但整个片段文本在导航标题之上。 The navigation bar is below the fragment so text in fragment can be seen above navigation header

0 个答案:

没有答案