简体   繁体   English

如何整合工具栏

[英]How can I integrate a Toolbar

I'm trying to integrate a Toolbar to my existing App. 我正在尝试将工具栏集成到我现有的应用程序中。 I have the problem that in every tutorial I've found they're using a class like this: 我遇到的问题是,在每个教程中我都发现他们正在使用这样的类:

public class MyActivity extends AppCompatActivity 

But I do already have a public class with an 'extends' in it. 但是我已经有了一个带有“扩展”功能的公共课程。 And as you know, in Java I can not extend two things in one class. 如您所知,在Java中,我不能在一个类中扩展两件事。

So: how can I solve this problem? 那么:我该如何解决这个问题?

One of the most interesting reasons for moving from ActionBar to Toolbar was to make Toolbar a flexible widget that can be used anywhere in the layouts. 从ActionBar移至Toolbar的最有趣的原因之一是使Toolbar成为可在布局中的任何位置使用的灵活小部件。 You can add a Toolbar to your XML layout file like any other widgets (Button, TextView etc.). 您可以像其他任何小部件(按钮,TextView等)一样,将工具栏添加到XML布局文件中。 I don't know what you are trying to make or do, So as to give a better suggestion. 我不知道您要做什么或做什么,以便提出更好的建议。 But, As a solution to your question, simply add these lines to your XML layout and that will do it: 但是,作为您的问题的解决方案,只需将以下行添加到XML布局中即可:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:layout_scrollFlags="scroll|enterAlways"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

Make sure to have Support library dependency in your build.gradle. 确保您的build.gradle中具有支持库依赖项。

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

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