【发布时间】:2014-09-16 08:10:14
【问题描述】:
我的应用中有不同的Activities,在所有这些中我都不想要Action Bar。我找不到如何禁用它。我试图找到一个属性将其应用于main_activity.xml,但到目前为止我没有找到任何东西。有人可以帮帮我吗?
【问题讨论】:
标签: java android android-actionbar
我的应用中有不同的Activities,在所有这些中我都不想要Action Bar。我找不到如何禁用它。我试图找到一个属性将其应用于main_activity.xml,但到目前为止我没有找到任何东西。有人可以帮帮我吗?
【问题讨论】:
标签: java android android-actionbar
哈哈,我前段时间也陷入了困境,所以我很高兴能帮助你找到一个至少对我有用的解决方案:)
您要做的是在 values/styles.xml 中定义一个新样式,使其看起来像这样
<resources>
<style name = "AppTheme" parent = "android:Theme.Holo.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name = "NoActionBar" parent = "@android:style/Theme.Holo.Light">
<item name = "android:windowActionBar">false</item>
<item name = "android:windowNoTitle">true</item>
</style>
</resources>
只有 NoActionBar 样式对您来说很有趣。最后你必须在 AndroidManifest.xml 中设置为你的应用程序的主题,所以它看起来像这样
<application
android:allowBackup = "true"
android:icon = "@drawable/ic_launcher"
android:label = "@string/app_name"
android:theme = "@style/NoActionBar" <!--This is the important line-->
>
<activity
[...]
我希望这会有所帮助,如果没有,请告诉我。
【讨论】:
item name = "android:windowActionBar"?我必须在没有 android: 前缀的情况下使用它才能使其工作。见this answer
有多种方法可以做到这一点。
在 Application 元素下方,您会找到主题标签。用这个替换它 -
android:theme="@android:style/Theme.NoTitleBar"
如果您使用的是 AppCompat,请使用 @android:style/Theme.AppCompat.NoActionBar。
这将为所有活动设置主题。如果您不需要特定活动中的操作栏,则在活动容器中设置主题,即
<activity android:theme="@android:style/Theme.NoTitleBar" ...>
您也可以在样式中设置android:theme="@android:style/Theme.NoTitleBar" 主题并稍后使用。
将此添加到res/values/styles.xml
<style name="NoActionBar" parent="@android:style/Theme.Holo.Light">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
然后在 Manifest 中将其设置为您的活动主题:
<activity android:theme="@style/NoActionBar" ... />
将此代码放在onCreate方法中设置内容视图之前 -
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getActionBar().hide();
如果您使用支持库,请使用getSupportActionBar()。
【讨论】:
如果您使用的是AppCompat,请在AndroidManifest.xml 中声明您的活动,如下所示:
<activity
android:name=".SomeActivity"
...
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
【讨论】:
考虑到每个人都在发布隐藏 ActionBar 的旧方法,这是在 AppCompat 支持库的样式中实现它的正确方法。如果您还没有,我强烈建议您继续前进。
只需删除 ActionBar
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
如果您使用的是 appcompat 支持库,这是隐藏 ActionBar 以全屏显示或开始在布局中实现工具栏的最简单且推荐的方法。
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Your Toolbar Color-->
<item name="colorPrimary">@color/primary</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">@color/primary_dark</item>
<!-- colorAccent is used as the default value for colorControlActivated,
which is used to tint widgets -->
<item name="colorAccent">@color/accent</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight, and colorSwitchThumbNormal. -->
</style>
然后改变你的工具栏颜色
<android.support.v7.widget.Toolbar
android:id=”@+id/my_awesome_toolbar”
android:layout_height=”wrap_content”
android:layout_width=”match_parent”
android:minHeight=”?attr/actionBarSize”
android:background=”?attr/primary” />
最后一点:您的 Activity 类应该扩展 AppCompatActivity
public class MainActivity extends AppCompatActivity {
<!--Activity Items -->
}
【讨论】:
你在 res/value/style.xml 中得到的默认样式,比如
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
就像你的活动标签中的下面一样,
android:theme="@style/AppTheme.NoActionBar"
【讨论】:
如果您希望大多数活动都有一个操作栏,您可能会从默认主题继承您的基本主题(默认情况下,Android Studio 会自动生成):
<!-- Base application theme. -->
<style name="AppTheme" 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>
然后为您的无栏活动添加一个特殊主题:
<style name="AppTheme.NoTitle" parent="AppTheme">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="actionBarTheme">@null</item>
</style>
对我来说,将 actionBarTheme 设置为 @null 是解决方案。
最后在清单文件中设置活动:
<activity ... android:theme="@style/AppTheme.NoTitle" ... >
【讨论】:
【讨论】:
打开 Manifest 并将属性 theme = "@style/Theme.AppCompat.Light.NoActionBar" 添加到您想要的活动中,而无需操作栏:
<application
...
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
...
</activity>
</application>
【讨论】:
我会尽量简单地展示它:
在 ANDROID MAINIFEST 文件中声明全屏活动:
<activity
android:name=".GameActivity"
android:label="@string/title_activity_game"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
现在在 Android 工作室(如果您使用它)在设计器中打开您的 Activity xml 文件(在我的示例中为 activity_game.xml)并选择主题(在设计器中点击带有小圆圈的按钮的手机上方),然后在左侧,然后是 NoTitleBar.Fullscreen。
希望它会有所帮助!
【讨论】:
请在styles.xml中找到默认主题
<!-- Base application theme. -->
<style name="AppTheme" 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>
并以这种方式更改父级
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
【讨论】:
android:theme="@style/Theme.MaterialComponents.Light.NoActionBar"
使用这个主题对我有用
【讨论】:
将此行放入您的Activity 中的Manifest:
<activity android:name=".MainActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
...
</activity>
并确保您没有在布局中添加 Toolbar
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
/>
【讨论】:
使用任何名称创建一个新样式,在我的例子中是“Theme.Alert.NoActionBar”
<style name="Theme.Alert.NoActionBar" parent="Theme.AppCompat.Dialog">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
像上面的代码一样将其父级设置为“Theme.AppCompat.Dialog”
打开 AndoridManifest.xml 并像这样自定义您想要显示为警报对话框的活动
<activity
android:excludeFromRecents="true"
android:theme="@style/Theme.Alert.NoActionBar"
android:name=".activities.UserLoginActivity" />
在我的应用程序中,我想将我的用户登录活动显示为警报对话框,这些是我使用的代码。希望这对你有用!!!
【讨论】:
真的很简单只需转到您的styles.xml 将父主题更改为
Theme.AppCompat.Light.NoActionBar 或 Theme.AppCompat.NoActionbar 你就完成了.. :)
【讨论】: