【发布时间】:2017-08-01 19:16:25
【问题描述】:
我正在开发一个应用程序(作为我的第一个应用程序),在这个应用程序中,我已经像这样声明了我的 navigationView:
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/navigation_drawer"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/drawer_menu"
app:headerLayout="@layout/drawer_header">
在我的 mainactivity.java 中有:
public class MainActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener{
...
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Log.d("Navigation","Item selected : "+ item.getItemId());
int id = item.getItemId();
switch (id){
case R.id.about:
Toast.makeText(getApplicationContext(),"About Us
elected",Toast.LENGTH_SHORT).show();
break;
case R.id.help:
Toast.makeText(getApplicationContext(),
"Help",Toast.LENGTH_SHORT).show();
break;
case R.id.setting:
Toast.makeText(getApplicationContext(),
"Settings",Toast.LENGTH_SHORT).show();
break;
case R.id.EditProfile:{
Intent intent= new Intent(this , EditProfile.class);
startActivity(intent);
}
default:
break;
}
DrawerLayout drawerLayout= (DrawerLayout)
findViewById(R.id.drawer_layout);
drawerLayout.closeDrawer(GravityCompat.START);
return false;
}
根据this,我已经扩展了ActionBarDrawerToggle,并在其中使用了onDrawerOpened 函数
drawerView.bringToFront();
drawerView.getParent().requestLayout();
但是当我点击导航视图项时,onNavigationItemSelected 函数仍然不会触发。
是因为我添加了标题视图吗??
此方法在我将标题视图添加到导航视图之前有效(我没有在将标题添加到导航视图之前对其进行测试。
而且我认为在 navigationView 标头中有一些按钮和 TextView 是很有用的。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
android:id="@+id/drawer_header"
android:background="@drawable/drawer_bg"
android:layout_width="match_parent"
android:padding="4dp"
android:layout_height="200dp">
<RelativeLayout
android:gravity="center_vertical"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/avatar"
android:id="@+id/AvatarImageView"
android:layout_centerHorizontal="true" />
<Button
android:text="Signup"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_alignParentRight="true"
android:id="@+id/header_signup"
android:textSize="12sp"
android:background="@drawable/signup_button"
android:textStyle="normal|bold" />
</RelativeLayout>
<TextView
android:textColor="@color/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:id="@+id/header_username"
android:textAlignment="center" />
<TextView
android:text="Logout"
android:textColor="@color/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_centerHorizontal="true"
android:textAlignment="center"
android:layout_height="wrap_content"
android:id="@+id/header_logout"
/>
</LinearLayout>
【问题讨论】:
标签: android