Navigation Drawer Header is not hiding

南楼画角 提交于 2019-12-23 03:32:52

问题


I tried to create one simple app with Drawer Menu. I created a project with Android Studio and select Navigation Drawer Layout. I'm trying to hide navigation drawer header and put it on toolbar... I found the way to put on toolbar but I can't find hide RED part of header in picture. Can you help me please.

Here is my layout page xml codes

<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"
    android:layout_marginTop="?attr/actionBarSize" //With this code i put menu under toolbar
    app:menu="@menu/activity_main_drawer" />

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

Here is my MainActivity.java

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();
    }
}



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

    if (id == R.id.nav_first_layout) {
        fragmentManager.beginTransaction().replace(R.id.content_frame,new FirstFragment()).commit();
    } else if (id == R.id.nav_second_layout) {
        fragmentManager.beginTransaction().replace(R.id.content_frame,new SecondFragment()).commit();
    } else if (id == R.id.nav_third_layout) {
        fragmentManager.beginTransaction().replace(R.id.content_frame,new ThirdFragment()).commit();
    }   else if (id == R.id.nav_home) {
        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        startActivity(intent);
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
}

How can I hide that red part??


回答1:


Set fitsystemwindow=false under android.support.design.widget.NavigationView in xml.




回答2:


Use this In your MainActivity

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


    View header = navigationView.getHeaderView(0);
         header.setVisibility(View.GONE);

OR if you not wana use that check bolew
In your xml file of main activity you should have NavigationView like this

<android.support.design.widget.NavigationView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/navigation_view_I"
        android:layout_gravity="start"
       android:fitsSystemWindows="true"
        app:menu="@menu/drawer_menu"
        app:headerLayout="@layout/navigation_drawer_header">

Remove last line from the above NavigationView which is this

app:headerLayout="@layout/navigation_drawer_header"



回答3:


Add below code inside of onCreateView() of NavigationDrawer Fragment class

View headerView= LayoutInflater.from(this).inflate(R.layout.drawer_header, null);
 navigationView.addHeaderView(headerView);
 navigationView.getHeaderView(0).setVisibility(View.GONE);

Note:- relace R.layout.drawer_header by your header view resource file.



来源:https://stackoverflow.com/questions/38497805/navigation-drawer-header-is-not-hiding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!