簡體   English   中英

與相對布局一起使用時,導航抽屜不起作用

[英]Navigation Drawer not working when used with relative layout

我有一個在我的活動中使用 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 時,單擊導航抽屜中的某些內容只會關閉抽屜而不會執行任何操作。

您已為TextView ConstraintLayout屬性,而您沒有ConstraintLayout

但是,您在DrawerLayout使用了RelativeLayout 你的內容應該放在CoordinatorLayout而不是DrawerLayout

無論如何,請確保您將CoordinatorLayout作為此文件中的根標記: app_bar_home然后放置您的內容(在那里沒有ConstraintLayout屬性(如果您沒有ConstraintLayout ))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM