簡體   English   中英

Android - 右側的導航抽屜是否可行?

[英]Android - Is Navigation Drawer from right hand side possible?

http://developer.android.com/training/implementing-navigation/nav-drawer.html

根據這個文檔,它沒有說明是否可以從右側實現抽屜。 有可能嗎? :(

NavigationDrawer 可以配置為從左側、右側或兩者拉出。 關鍵是 XML 聲明中抽屜的出現順序,以及 layout_gravity 屬性。 下面是一個例子:

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:baselineAligned="false" >
    </FrameLayout>

    <!-- Left drawer -->

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:choiceMode="singleChoice" />

    <!-- Right drawer -->

    <ListView
        android:id="@+id/right_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:choiceMode="singleChoice" />
</android.support.v4.widget.DrawerLayout>

這是抽屜上的文檔,您似乎可以將其配置為從左側或右側拉出。

抽屜的定位和布局是使用 android:layout_gravity 屬性控制的,子視圖對應於您希望抽屜出現的視圖的哪一側:左側或右側。 (或在支持布局方向的平台版本上開始/結束。)

http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html

我的應用程序因“未找到重力左側的抽屜視圖”錯誤而崩潰。

所以將其添加到 onOptionsItemSelected 中:

if (item != null && item.getItemId() == android.R.id.home) {
        if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
            mDrawerLayout.closeDrawer(Gravity.RIGHT);
        } else {
            mDrawerLayout.openDrawer(Gravity.RIGHT);
        }
    }

添加到https://stackoverflow.com/a/21781710/437039解決方案。

如果您使用的是由 Android Studio 創建的 Navigation Drawer 項目,則onOptionsItemSelected情況會發生變化。 由於他們創建了子類,因此您必須使用此代碼

if (item != null && id == android.R.id.home) {
        if (mNavigationDrawerFragment.isDrawerOpen(Gravity.RIGHT)) {
            mNavigationDrawerFragment.closeDrawer(Gravity.RIGHT);
        } else {
            mNavigationDrawerFragment.openDrawer(Gravity.RIGHT);
        }
        return true;
}

下一個。 NavigationDrawerFragment類中,您必須創建 3 個方法:

方法一

public boolean isDrawerOpen(int gravity) {
    return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(gravity);
}

方法二

public void closeDrawer(int gravity) {
    mDrawerLayout.closeDrawer(gravity);
}

方法三

public void openDrawer(int gravity) {
    mDrawerLayout.openDrawer(gravity);
}

只有現在,右側抽屜才會起作用。

您可以使用 Material design 中的NavigationView 例如:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

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

然后使用這些代碼@amal 我認為這會幫助你。 XML:

<!-- Framelayout to display Fragments -->

<FrameLayout
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="@drawable/counter_bg" >

        <ImageView
            android:id="@+id/iconl"
            android:layout_width="25dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/iconr"
            android:layout_width="25dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="17dp"
            android:src="@drawable/ic_launcher" />
    </RelativeLayout>
</FrameLayout>

<!-- Listview to display slider menu -->

<ListView
    android:id="@+id/list_slidermenu"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/list_background"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector" />

<ListView
    android:id="@+id/list_slidermenu2"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:background="@color/list_background"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector" />

//設置需要的圖片

活動代碼:

ImageView iconl,iconr;

private DrawerLayout mDrawerLayout;
private ListView mDrawerList,mDrawerList2;

ImageView iconl,iconr;

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

    iconl = (ImageView)findViewById(R.id.iconl);
    iconr = (ImageView)findViewById(R.id.iconr);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
    mDrawerList2 = (ListView) findViewById(R.id.list_slidermenu2);
    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
    mDrawerList2.setOnItemClickListener(new SlideMenuClickListener());
    iconl.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mDrawerLayout.openDrawer(Gravity.START);
            mDrawerLayout.closeDrawer(Gravity.END);
        }
    });
    iconr.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mDrawerLayout.openDrawer(Gravity.END);
            mDrawerLayout.closeDrawer(Gravity.START);
        }
    });
}
}

在這里,您可以為兩個列表設置自己的列表適配器,並在項目單擊時調用 displayView(position); 您可以將片段添加到框架布局的方法。

/**
 * Diplaying fragment view for selected nav drawer list item
 * */
private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new HomeFragment();
        break;


    default:
        break;
    }

    if (fragment != null) 
    {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
        .replace(R.id.frame_container, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);

        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

我知道這是一個老問題,但對於那些仍在尋找答案的人:

對的,這是可能的。 請在下面的鏈接上查看我的回答:

https://stackoverflow.com/a/19358114/1572408

要從屏幕右側設置導航抽屜,請將導航視圖的抽屜布局設為父級,並將導航視圖的布局重力設置為右側。

可以從右側導航抽屜。 這比看起來容易。 在我看來,最簡單的解決方案是:

  1. 擴展 DrawerLayout class 並覆蓋 open() 和 close() 函數,如下所示

     class EndDrawerLayout: DrawerLayout { constructor(context: Context): super(context) constructor(context: Context, attrs: AttributeSet?): super(context, attrs) constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int): super(context, attrs, defStyleAttr) override fun open() = openDrawer(GravityCompat.END) override fun close() = closeDrawer(GravityCompat.END) }
  2. 在 XML 中使用自定義 DrawerLayout

  3. 設置 NavigationView 屬性 android:layout_gravity="end"

     <com.custom.myapplication.ui.EndDrawerLayout 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"> <include android:id="@+id/app_bar_main" layout="@layout/app_bar_main" 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="end" android:fitsSystemWindows="true" app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" /> </com.custom.myapplication.ui.EndDrawerLayout>
  4. 享受

將此代碼寫入 Main.java 中,im1 是 xml 文件右上角的導航欄圖標。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    im1=findViewById(R.id.humburgericon);
    im1.setOnClickListener(new View.OnClickListener() {
        @SuppressLint("WrongConstant")
        @Override
        public void onClick(View v) {
              drawerLayout.openDrawer(Gravity.END);});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM