我尝试使用抽屉菜单创建一个简单的应用程序。我用Android Studio创建了一个项目并选择了Navigation Drawer Layout。我试图隐藏导航抽屉头并将其放在工具栏上......我找到了放在工具栏上的方法,但我找不到图片中隐藏红色部分的标题。你能帮我吗?
这是我的布局页面xml代码
<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>
这是我的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;
}
}
如何隐藏红色部分?
答案 0 :(得分:2)
在xml中的 android.support.design.widget.NavigationView 下设置fitsystemwindow=false
。
答案 1 :(得分:1)
在 NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View header = navigationView.getHeaderView(0);
header.setVisibility(View.GONE);
NavigationView
如果你没有使用那个检查bolew
在主要活动的xml文件中,您应该<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">
喜欢这个
NavigationView
从上面的app:headerLayout="@layout/navigation_drawer_header"
删除最后一行,这是
Cursor addrCursor = sResolver.query(Uri.parse("content://mms-sms/canonical-address/" + recipientId), null,null, null, null);
if (addrCursor.moveToFirst()) {
String number = addrCursor.getString(0); // we got number here
number = number.replace(" ", "");
number = number.replace("-", "");
Contact c = db.findContactByNumber(number);
return c;
} else {
return null;
}
答案 2 :(得分:0)
在NavigationDrawer Fragment类的<{1}}中添加以下代码
onCreateView()
注意: - 通过标题视图资源文件重新连接R.layout.drawer_header。