与相对布局一起使用时,导航抽屉不起作用

时间:2018-10-15 07:36:37

标签: android navigation-drawer android-navigation-drawer

我有一个工作的导航抽屉,该抽屉在我的活动中使用DrawerLayout。问题是我无法在其中放置内容,因此我在抽屉式布局中使用了相对布局。如果这样做,导航抽屉将不响应任何操作。它只是打开和关闭。

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_home"
    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_navigation"
    app:menu="@menu/activity_navigation_drawer" />

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

    <TextView
        android:id="@+id/info"
        android:layout_width="298dp"
        android:layout_height="40dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="58dp"
        android:layout_marginLeft="58dp"
        android:layout_marginTop="97dp"
        android:layout_marginEnd="55dp"
        android:layout_marginRight="55dp"
        android:layout_marginBottom="522dp"
        android:text="NCMH - Doctor Dashboard"
        android:textAlignment="center"
        android:textSize="25sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.483"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.182" />
</RelativeLayout>

这是我的活动:

public class DoctorActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

DrawerLayout drawer;
NavigationView navigationView;
Toolbar toolbar = null;
FirebaseAuth firebaseAuth;
FloatingActionButton fab;

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

    firebaseAuth = FirebaseAuth.getInstance();

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fabmenu);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(DoctorActivity.this, TextChat.class));
        }
    });

    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) 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.menu, 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.
    switch (item.getItemId()) {
        case R.id.profileMain: {
            startActivity(new Intent(DoctorActivity.this, MainActivity.class));
            break;
        }
        case R.id.profileMenu: {
            startActivity(new Intent(DoctorActivity.this, ProfileActivity.class));
            break;
        }
        case R.id.logoutMenu: {
            firebaseAuth.signOut();
            finish();
            startActivity(new Intent(DoctorActivity.this, Login.class));
            break;
        }
    }
    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    //here is the main place where we need to work on.
    int id = item.getItemId();
    switch (id) {

        case R.id.profile:
            Intent p = new Intent(DoctorActivity.this, ProfileActivity.class);
            startActivity(p);
            break;
        case R.id.calendar:
            Toast.makeText(this, "Calendar", Toast.LENGTH_SHORT).show();
            break;
        case R.id.categories:
            Toast.makeText(this, "Categories", Toast.LENGTH_SHORT).show();
            break;
        case R.id.about:
            Toast.makeText(this, "Abous Us", Toast.LENGTH_SHORT).show();
            break;
        case R.id.contact:
            Toast.makeText(this, "Contact", Toast.LENGTH_SHORT).show();
            break;

    }


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

}

如果删除相对布局,则导航抽屉可以正常工作。一切正常,但是每当我添加相对布局和textview时,从导航抽屉中单击某些内容只会关闭抽屉,而不执行任何操作。

1 个答案:

答案 0 :(得分:0)

您已赋予TextView ConstraintLayout属性,而您没有ConstraintLayout

但是,您在RelativeLayout内使用了DrawerLayout 。您的内容应放置在CoordinatorLayout 内,而不是DrawerLayout内。

无论如何,请确保您以CoordinatorLayout作为此文件中的根标记:app_bar_home然后将内容放置在其中(没有ConstraintLayout属性(如果您没有{{ 1}}))