簡體   English   中英

如何在設備屏幕的右側設置抽屜布局圖標?

[英]how to set Drawer Layout icon in right side of the device screen?

我已經創建了抽屜布局示例應用程序,它工作正常,我的問題是抽屜布局從右到左完美地工作但我試圖將圖標左側移動到右側但是它不起作用給我你的建議.. !!! 這有可能嗎?

在此輸入圖像描述

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="right" >

    <!-- The main content view -->

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         />
    <!-- The navigation drawer -->

    <ListView
        android:id="@+id/drawer_list"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:background="#111"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

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

也許現在為時已晚,但您可以使用默認菜單解決此問題。

創建res/menu/my_right_side_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/btnMyMenu"
        android:icon="@drawable/ic_drawer"
        android:title="Right Side Menu"
        myapp:showAsAction="always"/>
</menu>

然后在Activity中的onCreateOptionsMenu()中添加菜單

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    int menuToUse = R.menu.my_right_side_menu;

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(menuToUse, menu);

    return super.onCreateOptionsMenu(menu);
}

接下來,在ActionBarDrawerToggle處理菜單項的click事件

mDrawerToggle = new ActionBarDrawerToggle(this,  mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {

        @Override
        public boolean onOptionsItemSelected(android.view.MenuItem item) {
            if (item != null && item.getItemId() == R.id.btnMyMenu) {
                if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
                    mDrawerLayout.closeDrawer(Gravity.RIGHT);
                } else {
                    mDrawerLayout.openDrawer(Gravity.RIGHT);
                }
                return true;
            }
            return false;
        }
    };

最后不要忘記隱藏ActionBar Home按鈕

getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);

此圖標表示導航菜單,其設計必須位於屏幕的左側。 根據指南,我們可以在右側有一個導航抽屜,但是它應該用於修改內容(例如過濾器)。 出於所有這些目的,您可能希望使用ActionbarItem,並在屏幕的右上角放置一個ActionItem。 單擊該操作項將打開或關閉右側導航抽屜。

但可以肯定的是,根據設計,這個代表導航的動畫三線菜單圖標應位於左側。

僅供參考,要將導航抽屜放在右側,您必須更改導航抽屜的重力,如下所示:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/main_background" >

    <!-- The main content view -->

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>
    <!-- The navigation drawer -->

    <LinearLayout
        android:id="@+id/right_drawer"
        android:layout_width="280dp"
        android:layout_gravity="end"
        android:layout_height="match_parent"
        android:orientation="vertical" />

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

此外,在這種情況下,您真的非常希望右側的導航菜單圖標使用自定義標題布局或ActionBarSherlock等庫來編輯它。

我希望這有幫助!

如果您使用AppBarLayout和工具欄,請執行以下操作

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        android:layoutDirection="rtl">

        <android.support.v7.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" />


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

注意:android:layoutDirection =“rtl”

這是一個類似漏洞的解決方案,適用於我的情況。

toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar0, R.string.open_nav, R.string.close_nav);

我為它編了一個工具欄。

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_marginRight="40dp"
            android:layout_marginEnd="40dp"
            android:background="@color/colorPrimary">
            <!-- this is your general toolbar-->
            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="right|end|center_vertical"
                android:text="Mytitle"/>

        </android.support.v7.widget.Toolbar>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar0"
            android:layout_width="40dp"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="right|end"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
            <!-- this is your nav-button toolbar-->

    </FrameLayout>

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

並為它設置onclicklistener:

toolbar0.setNavigationOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (drawer.isDrawerOpen(Gravity.RIGHT)) {
                drawer.closeDrawer(Gravity.RIGHT);
            } else {
                drawer.openDrawer(Gravity.RIGHT);
            }
        }

    });

如果您不想使用工具欄或者不需要像我這樣的工具欄,您有兩種選擇,只需在活動的右上角添加ImageButton就可以了

<android.support.v4.widget.DrawerLayout   
  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right" >

       <!-- The main content view -->
        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

        <ImageButton
            android:id="@+id/account_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:padding="15dp"
            android:background="@null"
            android:src="@drawable/ic_menu_white_24dp" />

    </RelativeLayout>

<!-- The navigation drawer -->

<ListView
    android:id="@+id/drawer_list"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:background="#111"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp" />

然后簡單地在其上添加一個監聽器並使用drawerLayout.isDrawerOpen()來檢查正常if條件下抽屜的當前狀態,然后打開和關閉抽屜使用drawerLayout.openDrawer( )和drawerLayout.closeDrawer(); 對於你可以傳遞的每個方法,無論你的情況下的引力還是抽象視圖都是ListView,所以它應該像這樣的抽屜_layout.isDrawerOpen(drawer_list);

開發人員指南

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

這意味着,你可以通過以下方式實現:

<DrawerLayout
    android:layout_gravity="right">
</DrawerLayout>

編輯

根據創建導航抽屜

抽屜視圖(ListView)必須使用android:layout_gravity屬性指定其水平重力。 要支持從右到左(RTL)語言,請使用“start”而不是“left”指定值(因此當布局為RTL時,抽屜將顯示在右側)。

所以你應該這樣做:

<DrawerLayout
    android:layout_gravity="start">
</DrawerLayout>

暫無
暫無

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

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