android - 导航视图在 Android 中始终可见
问题描述
我想通过底部应用栏单击打开导航视图。但导航视图始终可见。
我的布局 xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
android:theme="@style/ThemeOverlay.MaterialComponents.Light"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="132dp"
android:background="@drawable/login_bg2_gradient"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView5"
android:layout_width="45dp"
android:layout_height="44dp"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="spread_inside"
app:srcCompat="@drawable/logo" />
<TextView
android:id="@+id/textView"
android:layout_width="91dp"
android:layout_height="23dp"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="SAMET DAĞ"
app:layout_constraintBottom_toTopOf="@+id/textView7"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView5" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="22dp"
android:text="8 Şubat 2019 11:05"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView8"
android:layout_width="69dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="4dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="MOSTA"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<ImageView
android:id="@+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="100dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.972"
app:layout_constraintStart_toEndOf="@+id/textView8"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_menu_manage" />
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true"
app:layout_constraintEnd_toEndOf="parent"
app:menu="@menu/profile_side_menu" />
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/coordinatorlayout"
android:layout_width="0dp"
android:layout_height="89dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/nav_view"
app:layout_constraintVertical_bias="0.938">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottombar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:backgroundTint="#949494"
app:buttonGravity="bottom|top"
app:fabAlignmentMode="center"
app:navigationIcon="@drawable/ic_menu_black_24dp"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="606dp"
/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/bottom_float_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shift_change"
android:src="@drawable/shift_change"
app:backgroundTint="@android:color/darker_gray"
app:fabCustomSize="50dp"
app:layout_anchor="@+id/bottombar"
app:layout_constraintBottom_toTopOf="@id/bottombar"
app:layout_constraintTop_toBottomOf="@+id/bottombar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.drawerlayout.widget.DrawerLayout>
我的Java代码:
package com.stawiz;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.GravityCompat;
import androidx.core.view.ViewCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import android.system.Os;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import com.google.android.material.bottomappbar.BottomAppBar;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.navigation.NavigationView;
import com.mxn.soul.flowingdrawer_core.ElasticDrawer;
import com.mxn.soul.flowingdrawer_core.FlowingDrawer;
import java.util.ArrayList;
import java.util.List;
public class MainMenu extends AppCompatActivity {
FloatingActionButton floatingActionButton;
BottomAppBar bottomAppBar;
CoordinatorLayout coordinatorLayout;
DrawerLayout drawerLayout;
Button btnLogOut,btnEditProfile;
NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
floatingActionButton = findViewById(R.id.bottom_float_button);
bottomAppBar = findViewById(R.id.bottombar);
coordinatorLayout=findViewById(R.id.coordinatorlayout);
drawerLayout=findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.nav_view);
setSupportActionBar(bottomAppBar);
bottomAppBar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { // It working when profile button pressed.
@Override
public boolean onMenuItemClick(MenuItem item) {
Log.e("asdas","--asdasd");
if(drawerLayout.isDrawerVisible(Gravity.RIGHT)){
Log.e("right","right");
}
if(drawerLayout.isDrawerVisible(Gravity.LEFT)){
Log.e("LEFT","LEFT");
}
if(drawerLayout.isDrawerVisible(GravityCompat.START)){
Log.e("START","START");
}
if(drawerLayout.isDrawerVisible(GravityCompat.END)){
Log.e("END","END");
}
return true;
}
});
bottomAppBar.setNavigationOnClickListener(new View.OnClickListener() {//It working when menu button pressed.
@Override
public void onClick(View v) {
BottomNavigationDrawer bottomNavigationDrawer = new BottomNavigationDrawer();
bottomNavigationDrawer.show(getSupportFragmentManager(),bottomNavigationDrawer.getTag());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.bottom_app_bar_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
/* switch (item.getItemId()){
case
}*/
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
finish();
} else {
super.onBackPressed();
}
}
}
我想显示导航视图 bottomAppBar.setOnMenuItemClickListener
。应用程序去那里,但导航视图总是显示。我只想用这个点击监听器显示导航视图。我怎样才能做到这一点 ?提前致谢。
解决方案
当您开始活动时,您正在打开它。请删除onCreate
方法中的行
drawerLayout.openDrawer(GravityCompat.START);
和改变
android:layout_gravity="end"
至
android:layout_gravity="start"
推荐阅读
- android - 为 Xamarin Android 应用程序构建通知
- javascript - JS中根据动态对象和值过滤数组
- javascript - Express.js 抛出错误;// 重新抛出非 MySQL 错误
- spring-boot - 使会话无效并从资源服务器访问数据后的 Spring Boot OAuth2 错误
- android - 如何将 ViewPager 的第一个 Fragment 替换为另一个 Fragment 并与前一个 Fragment 处于相同位置
- arrays - 如何计算这些大于数组中第 12 个元素的数字?
- python - 使用 YOLOv3/Tensorflow 检测时出现 KeyError
- laravel - 在laravel中调度每个作业后如何登录数据库?
- julia - 从 Plots.jl 中的终端运行时显示的绘图窗口
- r - 从 csv 文件可视化 R 中的箱线图