Java 没有 ActionBar 的 Android Activity
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25863676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Activity without ActionBar
提问by Lutscha
I have different Activities
in my App and in all of them I do not want the 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. Can somebody help me please?
Activities
我的应用程序中有不同的应用程序,并且在所有应用程序中我都不想要Action Bar
. 我找不到如何禁用它。我试图找到一个属性来应用它,main_activity.xml
但到目前为止我没有找到任何东西。有人可以帮我吗?
采纳答案by JRsz
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. At last you have to set is as your application's theme in the AndroidManifest.xml so it looks like this
只有 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
[...]
I hope this helps, if not, let me know.
我希望这会有所帮助,如果没有,请告诉我。
回答by JRsz
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!
希望它会有所帮助!
回答by Confuse
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. Replace it with this one -
在 Application 元素下方,您将找到主题标签。换成这个——
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.xml
添加这个 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>
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()
.
回答by CoolMind
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);
}
回答by Eugene H
Considering everyone is posting older ways of hiding the ActionBar here is the proper way of implementing it within styles for AppCompat support library. Which I highly suggest moving toward if you haven't already.
考虑到每个人都在这里发布隐藏 ActionBar 的旧方法,这是在 AppCompat 支持库的样式中实现它的正确方法。如果你还没有,我强烈建议你转向。
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 -->
}
回答by atablash
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" />
回答by Claude
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 actionBarThemeto @nullwas the solution.
对我来说,设置actionBarTheme到@null是解决办法。
Finally setup the activity in your manifest file:
最后在清单文件中设置活动:
<activity ... android:theme="@style/AppTheme.NoTitle" ... >
回答by Karthick
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!!!
在我的应用程序中,我想将我的用户登录活动显示为警报对话框,这些是我使用的代码。希望这对你有用!!!
回答by Mahmoud salah eldien saber
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>
回答by D. Sergeev
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">