如何使所有活动(MainActivity除外)同时具有ActionBar / Toolbar,并带有Activity的标题和后退按钮?

问题描述 投票:0回答:2

我有以下MainActivity

enter image description here

当我单击“ Congreso”按钮时,将显示以下活动:

enter image description here

当我单击“ Programa”按钮时,将出现以下活动:

enter image description here

[MainActivity以外的所有活动都有一个带有后退按钮的动作栏,该动作的标题每次都对齐在同一位置,并且动作栏的颜色相同。就我而言,我希望颜色是蓝色。我的问题是,有没有一种方法可以在应用程序中的单个位置上设置该行为,并将其应用于除MainActivity以外的所有活动,因此,如果有一天我想更改操作栏或与其标题保持一致,我不必一一参加所有活动并进行更改。

P.S .:我不知道屏幕快照中的内容是操作栏还是工具栏。如果您通过工具栏为我提供解决方案,也可以。

android set android-actionbar
2个回答
1
投票
请创建一个BaseActivity,使用BaseActivity中的操作栏,在其中处理“

Back Image Click”,然后在“单个活动”中设置相应的标题名称。所有活动将扩展BaseActivity,但不包括MainActivity


0
投票
在styles.xml中创建2个主题

<style name="NoActionAppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorPrimaryDark</item> <item name="android:windowNoTitle">true</item> <item name="android:windowActionBar">false</item> </style> <style name="ActionBarTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style>

清单文件中的下一个]

<activity android:name="MainActivity" android:label="title you want to set to action bar" android:theme="@style/NoActionAppTheme" /> <activity android:name="OtherActivity" android:label="title you want to set to action bar" android:parentActivityName=".MainActivity" // where you want go on clicking back android:theme="@style/ActionBarTheme" />

使用这种方法,您必须对styles.xml进行更改,其余部分将自行承担

以编程方式设置标题

getSupportActionBar().setTitle("Some Title You Want");

重要显示后退按钮

getSupportActionBar().setDisplayShowHomeEnabled(true);

© www.soinside.com 2019 - 2024. All rights reserved.