【问题标题】:Navigation Drawer RTL [duplicate]导航抽屉 RTL [重复]
【发布时间】:2018-07-09 11:32:11
【问题描述】:

我是这个网站的新手。
我现在正在 android studio 中开发应用程序,我正在寻找一个不错的菜单并找到了 Navigation Drawer,我搜索了如何创建自己的 Navigation,但我希望它位于屏幕的右侧。 我该怎么做?

关于复选框的相同问题,我在左侧有复选框,在右侧有文本/我希望它是相反的。

希望你能理解我的问题。

XML:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello Drawer"
        android:layout_gravity="center"
        android:gravity="center"/>
</LinearLayout>

<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:menu="@menu/navigation_menu"
    android:layout_gravity="right">
</android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

Java:

private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
    mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);

    mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    if(mToggle.onOptionsItemSelected(item))
    {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

【问题讨论】:

  • 这个我试过了,不行。
  • 然后提供一些代码,你到目前为止做了什么
  • 我该怎么做..? :(
  • stackoverflow.com/help 使用此链接
  • 我做到了:)。你知道做这件事的简单方法吗?如果不是,我该如何解决我的问题?切换工具栏位于左侧,而菜单本身位于右侧。我希望他们两个都在右边。

标签: java android navigation


【解决方案1】:

关于右侧的导航抽屉...我记得在这个问题上苦苦挣扎。我从任何地方检查了许多答案。他们大多说:使用 setLayoutDirection 方法,该方法工作正常,但对于 api>=17。我执行以下操作:

0。支持库

 dependencies {
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:support-v4:25.3.1'
    compile 'com.android.support:design:25.3.1'
}

1。布局

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:openDrawer="end">

    <!--content of your Activity. Could be fragment container. Your toolbar is placed there-->
    <include
        layout="@layout/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="right|end">

        <LinearLayout
            android:id="@+id/navigationLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <!--navigation header. like user profile image &...-->
            <include
                layout="@layout/drawer_header"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <!--recycler of your navigation items-->
            <android.support.v7.widget.RecyclerView
                android:id="@+id/navigationRecycler"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </LinearLayout>

    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

ma​​in_content.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout 
        android:id="@+id/toolbarRL"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageButton
            android:id="@+id/toolbarHamburger"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginRight="10dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:background="@null"
            android:src="@drawable/hamburger" />

    </RelativeLayout>

... other stuff ...

</RelativeLayout>

2。活动

//Hamburger icon of your toolbar
            ImageButton hamburger= (ImageButton) findViewById(R.id.toolbarHamburger);
            hamburger.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v){
                    if (drawerLayout.isDrawerOpen(Gravity.RIGHT))
                        drawerLayout.closeDrawer(Gravity.RIGHT);
                    else
                        drawerLayout.openDrawer(Gravity.RIGHT);
                }
            });

当点击回收站的物品时,您还应该使用drawerLayout.closeDrawer(Gravity.RIGHT) 关闭抽屉。

关于CheckBox 不知道有没有办法切换text和CB的位置。但我不会费心去寻找它。我宁愿使用包含 CB & TV 的 linearLayout :)

或者,如果我看到它重复,我会创建一个自定义视图。

CB:复选框,TV:TextView

【讨论】:

  • 我试过这样做,我不明白我应该把菜单按钮放在哪里?是 ImageButton 吗?
  • 如果菜单按钮是指必须打开抽屉的按钮,是的。它的图像按钮。我现在添加了它的布局
  • 非常感谢,我会试试这个,让你知道。背景是图标菜单?还是 thr scr?
  • 你是 wlc :) 我希望它对你有用。背景设置为空。 @drawable/hamburger 是图标的图片
猜你喜欢
  • 2015-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多