没有操作栏的 Android 活动

我有不同的 Activities在我的应用程序,在他们所有我不想要的 Action Bar。我找不到解除它的方法。我试图找到一个属性应用到 main_activity.xml,但到目前为止,我没有找到任何东西。有人能帮帮我吗?

158166 次浏览

哈哈,我之前也遇到过这种情况,所以我很高兴我能帮你解决这个问题,至少对我来说是这样的:)

您需要做的是在 value/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 中将 is 设置为应用程序的主题,使其看起来如下所示

<application
android:allowBackup = "true"
android:icon = "@drawable/ic_launcher"
android:label = "@string/app_name"
android:theme = "@style/NoActionBar"   <!--This is the important line-->
>
<activity
[...]

我希望这有帮助,如果没有,让我知道。

我会尽量表现得简单一些:

在 ANDROID 主文件中声明全屏活动:

<activity
android:name=".GameActivity"
android:label="@string/title_activity_game"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">

现在在 Android 工作室(如果你使用它)打开你的 Activity xml 文件(在我的例子 Activity _ game.xml) ,选择主题(在移动电话上面的设计师点击小圆按钮) ,然后选择左边的 Mainfest 主题,然后是 NoTitleBar。全屏。

希望能有所帮助!

有多种方法可以做到这一点。

1)更改 Android Manifest 中的主题

在 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"主题,并在以后使用它。


2)创建自定义主题

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>

然后将其设置为您的活动的主题在清单:

<activity android:theme="@style/NoActionBar" ... />

3)动态移除操作栏

将此代码放入 onCreate方法 在设置内容视图之前-中

getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getActionBar().hide();

如果您正在使用支持库,请使用 getSupportActionBar()

您还可以将继承从 ActionBarActivity 更改为已编写的 给你

更新

一个好的解决方案是 给你:

@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
}

考虑到每个人都在这里发布隐藏 ActionBar 的老方法,这是在 AppCompat 支持库的样式中实现它的正确方法。如果你还没有,我强烈建议你朝这个方向努力。

只需删除操作栏

<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 -->


}

如果您正在使用 AppCompat,请在 AndroidManifest.xml中声明您的活动如下:

<activity
android:name=".SomeActivity"
...
android:theme="@style/Theme.AppCompat.Light.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>

对我来说,将 动作主题设置为 @ null是解决方案。

最后,在清单文件中设置活动:

<activity ... android:theme="@style/AppTheme.NoTitle" ... >

创建任意名称的新样式,在我的示例中为“ 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 并像下面这样定制要显示为 Alert Dialog 的活动

    <activity
android:excludeFromRecents="true"
android:theme="@style/Theme.Alert.NoActionBar"
android:name=".activities.UserLoginActivity" />

在我的应用程序我想显示我的用户登录活动作为警报对话框,这些是我使用的代码。希望这对你有用! ! !

打开 Manifest 并在活动中添加主题 = “@style/Theme. AppCompat. Light.NoActionBar”属性,如果不想添加操作栏:

<application
...
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
...
</activity>
</application>

请在 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">

这真的很简单只要去你的 styles.xml改变父母的主题为任何一个 Theme.AppCompat.Light.NoActionBar或者 Theme.AppCompat.NoActionbar你就完成了. . :)

在 res/value/style.xml 中获得的默认样式如下

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

下面就是你的活动标签,

android:theme="@style/AppTheme.NoActionBar"

把这条线放在 ManifestActivity里:

<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"
/>
android:theme="@style/Theme.MaterialComponents.Light.NoActionBar"

用这个主题对我很有效