简体   繁体   English

没有 ActionBar 的 Android Activity

[英]Android Activity without ActionBar

I have different Activities in my App and in all of them I do not want the Action Bar .我的应用程序中有不同的Activities ,并且在所有Activities中我都不需要Action Bar I cannot find how to disable it.我找不到如何禁用它。 I have tried to find an attribute to apply it to the main_activity.xml but so far I did not find anything.我试图找到一个属性将它应用到main_activity.xml但到目前为止我没有找到任何东西。 Can somebody help me please?有人可以帮我吗?

Haha, I have been stuck at that point a while ago as well, so I am glad I can help you out with a solution, that worked for me at least :)哈哈,不久前我也被困在那个点上,所以我很高兴我可以帮助你解决一个至少对我有用的解决方案:)

What you want to do is define a new style within values/styles.xml so it looks like this您想要做的是在 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>

Only the NoActionBar style is intresting for you.只有 NoActionBar 样式对您感兴趣。 At last you have to set is as your application's theme in the AndroidManifest.xml so it looks like this最后,您必须在 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
    [...]

I hope this helps, if not, let me know.我希望这会有所帮助,如果没有,请告诉我。

There are multiple ways of doing this.有多种方法可以做到这一点。

1) Change the theme in Android Manifest 1) 更改 Android Manifest 中的主题

Below the Application element, you will find theme tag.在 Application 元素下方,您将找到主题标签。 Replace it with this one -换成这个——

android:theme="@android:style/Theme.NoTitleBar"

If you are using AppCompat, use @android:style/Theme.AppCompat.NoActionBar .如果您使用 AppCompat,请使用@android:style/Theme.AppCompat.NoActionBar

This will set the theme for all activities.这将为所有活动设定主题。 If you don't need the action bar in a specific acitivity, then set the theme in the activity container, ie如果特定活动中不需要操作栏,则在活动容器中设置主题,即

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

You may also set android:theme="@android:style/Theme.NoTitleBar" theme in the styles and use it later on.您也可以在样式中设置android:theme="@android:style/Theme.NoTitleBar"主题并稍后使用。


2) Create a Custom Theme 2)创建自定义主题

Add this in res/values/styles.xmlres/values/styles.xml添加这个

<style name="NoActionBar" parent="@android:style/Theme.Holo.Light">
     <item name="windowActionBar">false</item>
     <item name="android:windowNoTitle">true</item>
</style>

and then set it as your activity's theme in Manifest:然后在 Manifest 中将其设置为您的活动主题:

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

3) Dynamically Remove the Action Bar 3) 动态移除Action Bar

Put this code in the onCreate method before setting content view -在设置内容视图之前将此代码放在onCreate方法中 -

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

If you are using the support library use getSupportActionBar() .如果您使用支持库,请使用getSupportActionBar()

If you're using AppCompat , declare your activity in AndroidManifest.xml as follows:如果您使用AppCompat ,请在AndroidManifest.xml声明您的活动,如下所示:

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

Considering everyone is posting older ways of hiding the ActionBar here is the proper way of implementing it within styles for AppCompat support library.考虑到每个人都在这里发布隐藏 ActionBar 的旧方法,这是在 AppCompat 支持库的样式中实现它的正确方法。 Which I highly suggest moving toward if you haven't already.如果你还没有,我强烈建议你转向。

Simply removing the ActionBar只需删除 ActionBar

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>

If you are using the appcompat support libraries, this is the easiest and recommended way of hiding the ActionBar to make full screen or start implementing to toolbar within your layouts.如果您使用 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>

Then to change your toolbar color然后更改您的工具栏颜色

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

Last note: Your Activity class should extend AppCompatActivity最后一点:您的 Activity 类应该扩展 AppCompatActivity

public class MainActivity extends AppCompatActivity {

<!--Activity Items -->

}

Default style you getting in res/value/style.xml like您在 res/value/style.xml 中获得的默认样式,例如

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

And just the below like in your activity tag,就像在您的活动标签中一样,

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

If you want most of your activities to have an action bar you would probably inherit your base theme from the default one (this is automatically generated by Android Studio per default):如果您希望您的大部分活动都有一个操作栏,您可能会从默认主题继承您的基本主题(默认情况下,这是由 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>

And then add a special theme for your bar-less activity:然后为您的无酒吧活动添加一个特殊的主题:

<style name="AppTheme.NoTitle" parent="AppTheme">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="actionBarTheme">@null</item>
    </style>

For me, setting actionBarTheme to @null was the solution.对我来说,actionBarTheme设置@null是解决办法。

Finally setup the activity in your manifest file:最后在清单文件中设置活动:

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

You may also change inheritance from ActionBarActivity to Activity as written here .您还可以按照此处所述将继承从 ActionBarActivity 更改为 Activity。

UPDATE更新

A good solution is here :一个很好的解决方案在这里

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

open Manifest and add attribute theme = "@style/Theme.AppCompat.Light.NoActionBar" to activity you want it without actionbar :打开 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>

I will try to show it as simple as i can:我会尽量简单地展示它:

DECLARE FULL SCREEN ACTIVITY IN ANDROID MAINIFEST file:在 ANDROID MAINIFEST 文件中声明全屏活动:

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

Now in Android studio (if you using it) open your Activity xml file ( in my example activity_game.xml) in designer and select theme (above mobile phone in designer click button with small circle) and then select Mainfest Themes on the left side and then NoTitleBar.Fullscreen.现在在Android studio(如果您使用它)在设计器中打开您的Activity xml文件(在我的示例activity_game.xml中)并选择主题(设计器中的手机上方单击带有小圆圈的按钮),然后选择左侧的Mainfest Themes和然后是 NoTitleBar.Fullscreen。

Hope it will help!希望它会有所帮助!

Please find the default theme in styles.xml请在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>

And change parent this way并以这种方式改变父母

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

Put this line in your Activity in the Manifest :将此行放在您的Activity in the Manifest

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

and make sure you didn't put Toolbar in your layout并确保您没有将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"

使用这个主题对我有用

Create an new Style with any name, in my case "Theme.Alert.NoActionBar"创建一个任何名称的新样式,在我的例子中是“Theme.Alert.NoActionBar”

<style name="Theme.Alert.NoActionBar" parent="Theme.AppCompat.Dialog">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>

set its parent as "Theme.AppCompat.Dialog" like in the above code像上面的代码一样将其父项设置为“Theme.AppCompat.Dialog”

open AndoridManifest.xml and customize the activity you want to show as Alert Dialog like this打开 AndoridManifest.xml 并像这样自定义要显示为警报对话框的活动

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

in my app i want show my user login activity as Alert Dialog, these are the codes i used.在我的应用程序中,我想将我的用户登录活动显示为警报对话框,这些是我使用的代码。 hope this works for you!!!希望这对你有用!!!

这真的很简单只需转到你的styles.xml将父主题更改为Theme.AppCompat.Light.NoActionBarTheme.AppCompat.NoActionbar就完成了.. :)

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

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