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

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

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) :

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
    &lt;androidx.drawerlayout.widget.DrawerLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
        xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
        xmlns:tools=&quot;http://schemas.android.com/tools&quot;
        android:id=&quot;@+id/drawer_layout&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:fitsSystemWindows=&quot;true&quot;
//this lets drawer to be on right
        tools:openDrawer=&quot;end&quot;&gt;
    
        &lt;include
            layout=&quot;@layout/app_bar_lectures_graph&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot; /&gt;
    
        &lt;com.google.android.material.navigation.NavigationView
            android:id=&quot;@+id/nav_view&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;match_parent&quot;
            android:layout_gravity=&quot;start&quot;
            android:background=&quot;#fff&quot;
            android:fitsSystemWindows=&quot;true&quot;
            app:headerLayout=&quot;@layout/nav_header_lectures_graph&quot;
            app:menu=&quot;@menu/activity_lectures_graph_drawer&quot;
//this allows drawer to be on right
android:layout_gravity=&quot;end&quot; /&gt;
    
    &lt;/androidx.drawerlayout.widget.DrawerLayout&gt;

app bar layout (app_bar_lectures_graph.xml) :

 &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.systems.lectures.Lectures_graph&quot;&gt;

    &lt;com.google.android.material.appbar.AppBarLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:theme=&quot;@style/AppTheme.AppBarOverlay&quot;&gt;

        &lt;androidx.appcompat.widget.Toolbar
            android:id=&quot;@+id/toolbar&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;?attr/actionBarSize&quot;
            android:background=&quot;?attr/colorPrimary&quot;
            app:popupTheme=&quot;@style/AppTheme.PopupOverlay&quot; /&gt;

    &lt;/com.google.android.material.appbar.AppBarLayout&gt;

    &lt;include layout=&quot;@layout/content_lectures_graph&quot; /&gt;

&lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;

Drawer xml (activity_lectures_graph_drawer.xml) :

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.systems.lectures.Lectures_graph&quot;&gt;

    &lt;com.google.android.material.appbar.AppBarLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:theme=&quot;@style/AppTheme.AppBarOverlay&quot;&gt;

        &lt;androidx.appcompat.widget.Toolbar
            android:id=&quot;@+id/toolbar&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;?attr/actionBarSize&quot;
            android:background=&quot;?attr/colorPrimary&quot;
            app:popupTheme=&quot;@style/AppTheme.PopupOverlay&quot; /&gt;

    &lt;/com.google.android.material.appbar.AppBarLayout&gt;

    &lt;include layout=&quot;@layout/content_lectures_graph&quot; /&gt;

&lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;

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();

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:

确定