更改导航抽屉中汉堡图标的位置

huangapple go评论99阅读模式
英文:

change burger icon side for nav drawer

问题

我使用了 Android Studio 的侧边菜单抽屉模板。

问题:我想要整个抽屉显示在右边。

进展:我可以将抽屉布局的 android:layout_gravity 更改为 "end",但是我无法将操作栏中的三线图标(也称为架子、汉堡或导航抽屉图标)移动到右侧。

以下是提供更好理解我问题的可视化资源:
更改导航抽屉中汉堡图标的位置

以下是代码部分:

抽屉活动(Lecture_graph.java):

  1. public class Lectures_graph extends AppCompatActivity implements course_list.OnFragmentInteractionListener, lectures_graph.OnFragmentInteractionListener{
  2. // ... 省略其他代码 ...
  3. }

这是用于这个活动的 XML 布局(activity_lecture_graph.xml):

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:id="@+id/drawer_layout"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:fitsSystemWindows="true"
  9. tools:openDrawer="end">
  10. <!-- ... 省略其他代码 ... -->
  11. </androidx.drawerlayout.widget.DrawerLayout>

AppBar 布局(app_bar_lectures_graph.xml):

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".systems.lectures.Lectures_graph">
  8. <!-- ... 省略其他代码 ... -->
  9. </androidx.coordinatorlayout.widget.CoordinatorLayout>

抽屉 XML 布局(activity_lectures_graph_drawer.xml):

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".systems.lectures.Lectures_graph">
  8. <!-- ... 省略其他代码 ... -->
  9. </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) :

  1. public class Lectures_graph extends AppCompatActivity implements course_list.OnFragmentInteractionListener, lectures_graph.OnFragmentInteractionListener{
  2. private AppBarConfiguration mAppBarConfiguration;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_lectures_graph);
  7. Toolbar toolbar = findViewById(R.id.toolbar);
  8. setSupportActionBar(toolbar);
  9. DrawerLayout drawer = findViewById(R.id.drawer_layout);
  10. NavigationView navigationView = findViewById(R.id.nav_view);
  11. // Passing each menu ID as a set of Ids because each
  12. // menu should be considered as top level destinations.
  13. mAppBarConfiguration = new AppBarConfiguration.Builder(
  14. R.id.nav_course_list, R.id.nav_lectures_greph)
  15. .setDrawerLayout(drawer)
  16. .build();
  17. NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
  18. NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
  19. NavigationUI.setupWithNavController(navigationView, navController);
  20. }
  21. @Override
  22. public boolean onSupportNavigateUp() {
  23. NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
  24. return NavigationUI.navigateUp(navController, mAppBarConfiguration)
  25. || super.onSupportNavigateUp();
  26. }
  27. @Override
  28. public void onFragmentInteraction(Uri uri) {
  29. }
  30. }

XML for this activity (activity_lecture_graph.xml) :

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;androidx.drawerlayout.widget.DrawerLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:id=&quot;@+id/drawer_layout&quot;
  6. android:layout_width=&quot;match_parent&quot;
  7. android:layout_height=&quot;match_parent&quot;
  8. android:fitsSystemWindows=&quot;true&quot;
  9. //this lets drawer to be on right
  10. tools:openDrawer=&quot;end&quot;&gt;
  11. &lt;include
  12. layout=&quot;@layout/app_bar_lectures_graph&quot;
  13. android:layout_width=&quot;match_parent&quot;
  14. android:layout_height=&quot;match_parent&quot; /&gt;
  15. &lt;com.google.android.material.navigation.NavigationView
  16. android:id=&quot;@+id/nav_view&quot;
  17. android:layout_width=&quot;wrap_content&quot;
  18. android:layout_height=&quot;match_parent&quot;
  19. android:layout_gravity=&quot;start&quot;
  20. android:background=&quot;#fff&quot;
  21. android:fitsSystemWindows=&quot;true&quot;
  22. app:headerLayout=&quot;@layout/nav_header_lectures_graph&quot;
  23. app:menu=&quot;@menu/activity_lectures_graph_drawer&quot;
  24. //this allows drawer to be on right
  25. android:layout_gravity=&quot;end&quot; /&gt;
  26. &lt;/androidx.drawerlayout.widget.DrawerLayout&gt;

app bar layout (app_bar_lectures_graph.xml) :

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:layout_width=&quot;match_parent&quot;
  6. android:layout_height=&quot;match_parent&quot;
  7. tools:context=&quot;.systems.lectures.Lectures_graph&quot;&gt;
  8. &lt;com.google.android.material.appbar.AppBarLayout
  9. android:layout_width=&quot;match_parent&quot;
  10. android:layout_height=&quot;wrap_content&quot;
  11. android:theme=&quot;@style/AppTheme.AppBarOverlay&quot;&gt;
  12. &lt;androidx.appcompat.widget.Toolbar
  13. android:id=&quot;@+id/toolbar&quot;
  14. android:layout_width=&quot;match_parent&quot;
  15. android:layout_height=&quot;?attr/actionBarSize&quot;
  16. android:background=&quot;?attr/colorPrimary&quot;
  17. app:popupTheme=&quot;@style/AppTheme.PopupOverlay&quot; /&gt;
  18. &lt;/com.google.android.material.appbar.AppBarLayout&gt;
  19. &lt;include layout=&quot;@layout/content_lectures_graph&quot; /&gt;
  20. &lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;

Drawer xml (activity_lectures_graph_drawer.xml) :

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:layout_width=&quot;match_parent&quot;
  6. android:layout_height=&quot;match_parent&quot;
  7. tools:context=&quot;.systems.lectures.Lectures_graph&quot;&gt;
  8. &lt;com.google.android.material.appbar.AppBarLayout
  9. android:layout_width=&quot;match_parent&quot;
  10. android:layout_height=&quot;wrap_content&quot;
  11. android:theme=&quot;@style/AppTheme.AppBarOverlay&quot;&gt;
  12. &lt;androidx.appcompat.widget.Toolbar
  13. android:id=&quot;@+id/toolbar&quot;
  14. android:layout_width=&quot;match_parent&quot;
  15. android:layout_height=&quot;?attr/actionBarSize&quot;
  16. android:background=&quot;?attr/colorPrimary&quot;
  17. app:popupTheme=&quot;@style/AppTheme.PopupOverlay&quot; /&gt;
  18. &lt;/com.google.android.material.appbar.AppBarLayout&gt;
  19. &lt;include layout=&quot;@layout/content_lectures_graph&quot; /&gt;
  20. &lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;

Will be thankful for any help.

答案1

得分: 1

你可以使用 ActionBarDrawerToggle
尝试这个(Kotlin):

  1. val toggle = ActionBarDrawerToggle(
  2. this, drawer_layout, toolbar,
  3. R.string.navigation_drawer_open,
  4. R.string.navigation_drawer_close
  5. )
  6. toggle.isDrawerIndicatorEnabled = false
  7. toggle.setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp)
  8. toggle.setToolbarNavigationClickListener {
  9. if (drawer.isDrawerVisible(GravityCompat.START)) {
  10. drawer.closeDrawer(GravityCompat.START)
  11. } else {
  12. drawer.openDrawer(GravityCompat.START)
  13. }
  14. }
  15. drawer.addDrawerListener(toggle)
  16. toggle.syncState()

对于 Java:

  1. Toolbar toolbar = findViewById(R.id.toolbar);
  2. DrawerLayout drawer = findViewById(R.id.drawer_layout);
  3. ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
  4. this, drawer, toolbar,
  5. R.string.navigation_drawer_open,
  6. R.string.navigation_drawer_close
  7. );
  8. toggle.setDrawerIndicatorEnabled(false);
  9. toggle.setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp);
  10. toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
  11. @Override
  12. public void onClick(View v) {
  13. if (drawer.isDrawerVisible(GravityCompat.START)) {
  14. drawer.closeDrawer(GravityCompat.START);
  15. } else {
  16. drawer.openDrawer(GravityCompat.START);
  17. }
  18. }
  19. });
  20. drawer.addDrawerListener(toggle);
  21. toggle.syncState();
英文:

you can use ActionBarDrawerToggle.
try this (kotlin) :

  1. val toggle = ActionBarDrawerToggle(
  2. this, drawer_layout, toolbar,
  3. R.string.navigation_drawer_open,
  4. R.string.navigation_drawer_close
  5. )
  6. toggle.isDrawerIndicatorEnabled = false
  7. toggle.setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp)
  8. toggle.setToolbarNavigationClickListener {
  9. if (drawer .isDrawerVisible(GravityCompat.START)) {
  10. drawer .closeDrawer(GravityCompat.START)
  11. } else {
  12. drawer .openDrawer(GravityCompat.START)
  13. }
  14. }
  15. drawer .addDrawerListener(toggle)
  16. toggle.syncState()

for (Java)

  1. Toolbar toolbar = findViewById(R.id.toolbar);
  2. DrawerLayout drawer = findViewById(R.id.drawer_layout);
  3. ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
  4. this, drawer, toolbar,
  5. R.string.navigation_drawer_open,
  6. R.string.navigation_drawer_close
  7. );
  8. toggle.setDrawerIndicatorEnabled(false);
  9. toggle.setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp);
  10. toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
  11. @Override
  12. public void onClick(View v) {
  13. if (drawer .isDrawerVisible(GravityCompat.START)) {
  14. drawer .closeDrawer(GravityCompat.START);
  15. } else {
  16. drawer .openDrawer(GravityCompat.START);
  17. }
  18. }
  19. });
  20. drawer .addDrawerListener(toggle);
  21. toggle.syncState();

huangapple
  • 本文由 发表于 2020年4月8日 19:07:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/61099257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定