简体   繁体   English

Android工具栏上的所有活动

[英]Android toolbar to all the activities

I am new to Android. 我是Android新手。 I need to define the tool bar to all the activity. 我需要为所有活动定义工具栏。 For home screen I have some menu icons on toolbar. 对于主屏幕,我在工具栏上有一些菜单图标。

For other activities I need to have back button on toolbar with some selected menu icons. 对于其他活动,我需要在工具栏上具有一些选定菜单图标的后退按钮。 (visible true and false). (可见的是非)。

What's the best approach ? 最好的方法是什么?

  1. Define one toolbar and user it everywhere 定义一个工具栏,并随处使用
  2. Create toolbar every time for each activities. 每次为每个活动创建工具栏。

When I create new activities I don't want to create toolbar every time. 当我创建新活动时,我不想每次都创建工具栏。 What's the best way to have activities in inheritance way. 以继承方式进行活动的最佳方式是什么。 (define toolbar once and use it everywhere on activity screen)? (一次定义工具栏,并在活动屏幕上的任何地方使用它)?

Create a layout named toobar.xml 创建一个名为toobar.xml的布局

 <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
          />

Then include this in every activity layout file: 然后将其包括在每个活动布局文件中:

<include layout="@layout/toolbar" />

Then initiate in onCreate() of every activity: 然后在每个活动的onCreate()中启动:

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);

Create theme to remove default ActionBar: 创建主题以删除默认的ActionBar:

<style name="AppTheme.NoActionBar">
     <item name="windowActionBar">false</item>
     <item name="windowNoTitle">true</item>
 </style>

Set theme in manifest: 在清单中设置主题:

   <activity
      android:name=".MainActivity"
      android:label="@string/app_name"
      android:theme="@style/AppTheme.NoActionBar" />

Hope this helps. 希望这可以帮助。

创建工具栏并将其与片段配合使用,这是最简单的方法

You can use BaseActivity pattern. 您可以使用BaseActivity模式。 With it you can define toolbar in base and extend that in all other activities. 有了它,您可以在基础中定义工具栏,并将其扩展到所有其他活动中。 or you can change fragments at bottom part of toolbar using frame layout. 或者您可以使用框架布局在工具栏底部更改片段。

tahsinRupam,我必须将: <include layout="@layout/toolbar"/>更改为: <include android:layout="@layout/toolbar"/>

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

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