英文:
change burger icon side for nav drawer
问题
我使用了 Android Studio 的侧边菜单抽屉模板。
问题:我想要整个抽屉显示在右边。
进展:我可以将抽屉布局的 android:layout_gravity 更改为 "end",但是我无法将操作栏中的三线图标(也称为架子、汉堡或导航抽屉图标)移动到右侧。
以下是提供更好理解我问题的可视化资源:
以下是代码部分:
抽屉活动(Lecture_graph.java):
public class Lectures_graph extends AppCompatActivity implements course_list.OnFragmentInteractionListener, lectures_graph.OnFragmentInteractionListener{
// ... 省略其他代码 ...
}
这是用于这个活动的 XML 布局(activity_lecture_graph.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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="end">
<!-- ... 省略其他代码 ... -->
</androidx.drawerlayout.widget.DrawerLayout>
AppBar 布局(app_bar_lectures_graph.xml):
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
tools:context=".systems.lectures.Lectures_graph">
<!-- ... 省略其他代码 ... -->
</androidx.coordinatorlayout.widget.CoordinatorLayout>
抽屉 XML 布局(activity_lectures_graph_drawer.xml):
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
tools:context=".systems.lectures.Lectures_graph">
<!-- ... 省略其他代码 ... -->
</androidx.coordinatorlayout.widget.CoordinatorLayout>
感谢任何帮助。
英文:
I used a Android Studio Template for Side menu drawer.
Problem : I want the whole drawer to appear on right.
Progress : I can change the android:layout_gravity to "end" for drawer layout, but i cannot move the three-line-icon (also called shelf, burger or Navigation Drawer icon) in ActionBar to the right side.
Visual source to maybe provide better understanding of my problem:
Here comes the code :
Drawer activity (Lecture_graph.java) :
public class Lectures_graph extends AppCompatActivity implements course_list.OnFragmentInteractionListener, lectures_graph.OnFragmentInteractionListener{
private AppBarConfiguration mAppBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lectures_graph);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_course_list, R.id.nav_lectures_greph)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
@Override
public void onFragmentInteraction(Uri uri) {
}
}
XML for this activity (activity_lecture_graph.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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
//this lets drawer to be on right
tools:openDrawer="end">
<include
layout="@layout/app_bar_lectures_graph"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_lectures_graph"
app:menu="@menu/activity_lectures_graph_drawer"
//this allows drawer to be on right
android:layout_gravity="end" />
</androidx.drawerlayout.widget.DrawerLayout>
app bar layout (app_bar_lectures_graph.xml) :
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
tools:context=".systems.lectures.Lectures_graph">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_lectures_graph" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Drawer xml (activity_lectures_graph_drawer.xml) :
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
tools:context=".systems.lectures.Lectures_graph">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_lectures_graph" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Will be thankful for any help.
答案1
得分: 1
你可以使用 ActionBarDrawerToggle
。
尝试这个(Kotlin):
val toggle = ActionBarDrawerToggle(
this, drawer_layout, toolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
)
toggle.isDrawerIndicatorEnabled = false
toggle.setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp)
toggle.setToolbarNavigationClickListener {
if (drawer.isDrawerVisible(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START)
} else {
drawer.openDrawer(GravityCompat.START)
}
}
drawer.addDrawerListener(toggle)
toggle.syncState()
对于 Java:
Toolbar toolbar = findViewById(R.id.toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
);
toggle.setDrawerIndicatorEnabled(false);
toggle.setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp);
toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (drawer.isDrawerVisible(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
drawer.openDrawer(GravityCompat.START);
}
}
});
drawer.addDrawerListener(toggle);
toggle.syncState();
英文:
you can use ActionBarDrawerToggle
.
try this (kotlin) :
val toggle = ActionBarDrawerToggle(
this, drawer_layout, toolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
)
toggle.isDrawerIndicatorEnabled = false
toggle.setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp)
toggle.setToolbarNavigationClickListener {
if (drawer .isDrawerVisible(GravityCompat.START)) {
drawer .closeDrawer(GravityCompat.START)
} else {
drawer .openDrawer(GravityCompat.START)
}
}
drawer .addDrawerListener(toggle)
toggle.syncState()
for (Java)
Toolbar toolbar = findViewById(R.id.toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
);
toggle.setDrawerIndicatorEnabled(false);
toggle.setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp);
toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (drawer .isDrawerVisible(GravityCompat.START)) {
drawer .closeDrawer(GravityCompat.START);
} else {
drawer .openDrawer(GravityCompat.START);
}
}
});
drawer .addDrawerListener(toggle);
toggle.syncState();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论