简体   繁体   English

在片段视图中追加额外的工具栏

[英]Appending extra toolbar in fragment view

In my application on the dashboard, there is an application bar that looks something like the image below. 在仪表板上的应用程序中,有一个应用程序栏,看起来像下面的图像。

在此处输入图片说明

As shown in the image, I have a toolbar that currently shows the Navigation drawer. 如图所示,我有一个工具栏,当前显示“导航”抽屉。

From the navigation drawer, I can navigate to a fragment A that replaces the TAB area under the toolbar as shown in the image below. 从导航抽屉中,我可以导航到片段A,该片段将替换工具栏下的TAB区域,如下图所示。

在此处输入图片说明

Is there any way I can add two toolbars like this where I can show the navigation drawer icon and back button together inside a fragment? 有什么办法可以添加两个这样的工具栏,以便在片段中一起显示导航抽屉图标和后退按钮?

There is no best way to achieve what you are looking for, considering it goes against the Android Design Guidelines . 考虑到它违反了Android设计指南 ,因此没有最佳的方法来实现您的期望。 Although not explicitly stated, the navigation drawer icon and the back button icon are never displayed together. 尽管未明确说明,但导航抽屉图标和后退按钮图标永远不会一起显示。

The theory behind this design is that navigation should be intuitive and predictable, displaying two navigational icons next to each other detracts from an intuitive user interface. 该设计背后的理论是,导航应该直观且可预测,彼此相邻显示两个导航图标会损害直观的用户界面。 The back button should be displayed if there is something to go back to. 如果有需要返回的内容 ,应显示后退按钮。 Though, the need for persistent navigation can be addressed in two different ways. 但是,可以通过两种不同的方式解决持续导航的需求。 Take Google's Gmail app for example. 以Google的Gmail应用为例。

By default the NavigationView supports using a swipe from the left edge of the screen as a gesture to open the navigation drawer. 默认情况下, NavigationView支持使用屏幕左边缘的滑动作为打开导航抽屉的手势。 In the Gmail app, the navigation drawer icon is show while you are in any one of your inboxes. 在Gmail应用程序中,当您位于任何一个收件箱中时,都会显示导航抽屉图标。 As soon as a message is selected, the navigation drawer icon is replaced with the back button. 选择一条消息后,导航抽屉图标将替换为“ 后退”按钮。 Though, you will notice that you can still access the drawer using the gesture stated previously. 不过,您会注意到您仍然可以使用前面所述的手势访问抽屉。

带抽屉图标的Gmail 带后退按钮的Gmail

On larger screens, the Gmail app supports a miniature navigation drawer. 在较大的屏幕上,Gmail应用程序支持微型导航抽屉。 This drawer can remain in view without the need to display to navigational icons. 该抽屉可以保持在视图中,而无需显示导航图标。 Though this is a little more difficult to implement with the current support library, if you are looking to, this answer may help. 尽管使用当前的支持库很难实现这一点,但是如果您愿意的话, 此答案可能会有所帮助。

带永久抽屉的Gmail

Finally, to answer your question, there is no built-in way to display a back button and a navigation drawer icon together. 最后,要回答您的问题,没有内置的方法可以同时显示后退按钮和导航抽屉图标。 If you need to do so, the only way would be to use a "work-around", as you currently are. 如果您需要这样做,唯一的办法就是像现在一样使用“解决方法”。

Though, the only change I would probably make is to use an ImageButton rather than an ImageView . 不过,我可能唯一要做的更改是使用ImageButton而不是ImageView Then you can set the style of the button using style="?android:attr/actionButtonStyle" to mimic the look of an ActionBar button. 然后,您可以使用style="?android:attr/actionButtonStyle"设置按钮的样式,以模仿ActionBar按钮的外观。

Just to update, for the time being, to achieve the above what I have done is. 暂时进行更新,以实现上述目标。

I added a layout to my fragment file where I draw a custom back button like this: 我在片段文件中添加了一个布局,在其中绘制了一个自定义后退按钮,如下所示:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimaryDark20"
    android:orientation="horizontal"
    android:padding="15dp"
    android:weightSum="3">
    <ImageView
        android:id="@+id/backButton"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:layout_weight="0.2"
        android:src="@drawable/delete_icon" />
</LinearLayout>

When a person clicks on backImage, I call popBackStack . 当一个人单击backImage时,我将其称为popBackStack

   imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getFragmentManager().popBackStack();
        }
    });

The above code gives me the desired functionality. 上面的代码为我提供了所需的功能。

Note, for the above to work you need to make sure you are adding your fragmentTransaction to backstack as below 请注意,为使以上功能正常工作,您需要确保将fragmentTransaction添加到Backstack中,如下所示

final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.home_content_layout,NAME_OFYOUR_FRAGMENT,"nameOfYourFragment");
transaction.addToBackStack(null);
transaction.commit();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM