[英]Cannot hide title bar
I am new for Android development and I want to hide title bar in my application. 我是Android开发的新手,我想在应用程序中隐藏标题栏。
I tried everything I can to hide the bar but the application either stopped or had nothing changed. 我尽我所能隐藏栏,但是应用程序停止了或什么都没有更改。
I ran the demo on a virtual device.It's a 5.4" FWVGA phone. 我在虚拟设备上运行了演示。这是5.4英寸FWVGA电话。
I've also tried to add 我也尝试添加
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" 机器人:主题= “@安卓风格/ Theme.Holo.NoActionBar.Fullscreen”
or 要么
android:theme="@android:style/Theme.NoTitleBar(.Fullscreen)" 机器人:主题= “@安卓风格/ Theme.NoTitleBar(.Fullscreen)”
but unfortunately , the application stopped. 但不幸的是,该应用程序停止了。
Please tell me how to hide the title bar. 请告诉我如何隐藏标题栏。
Here is my java codes , layout file , AndroidManifest.xml and styles.xml. 这是我的Java代码,布局文件,AndroidManifest.xml和styles.xml。
package com.activitytest2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
public class FirstActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//1. getActionBar().hide();
//2. requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.first_layout);
Button button1 = (Button) findViewById(R.id.button_1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(FirstActivity.this,"You clicked button 1",Toast.LENGTH_SHORT).show();
}
});
}
}
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button 1"/>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.activitytest2">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".FirstActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
styles.xml
<resources>
<!-- 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>
</resources>
go to simple way. 去简单的方法。 just replace
getActionBar().hide();
只需替换
getActionBar().hide();
to getSupportActionBar().hide();
以获得
getSupportActionBar().hide();
I hope this solution helps you. 希望此解决方案对您有所帮助。
"@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.first_layout);"
No need to add getActionBar().hide();
无需添加
getActionBar().hide();
Just do this , 只是这样做,
Flag for the "no title" feature, turning off the title at the top of the screen.
标记“无标题”功能,关闭屏幕顶部的标题。
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
For test case , add this in your style section 对于测试用例,请将其添加到样式部分
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
and make sure below in manifest 并确保清单中的以下内容
android:theme="@style/AppTheme"
you can try with, 您可以尝试
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//code that displays the content in full screen mode
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
在onCreate()中将删除您的标题栏。
Just Try with, 尝试一下,
requestWindowFeature(Window.FEATURE_NO_TITLE);
before the setContentView
. 在
setContentView
之前。 like, 喜欢,
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.first_layout);
}
Are you using AppCompat activity for that u need AppCompat theme. 您是否正在使用AppCompat活动,因为您需要AppCompat主题。 use this Theme.AppCompat.Light.NoActionBar
使用此Theme.AppCompat.Light.NoActionBar
//add style file //添加样式文件
<style name="Theme.SlamBook.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar"></style>
//add in menifest //添加清单
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.SlamBook.NoActionBar">
<activity android:name=".FirstActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Replace your style with below:This will hide your actionbar too. 将样式替换为以下样式:这也将隐藏操作栏。
<!-- Base application theme. -->
<style name="AppTheme" 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/colorAccent</item>
<item name="android:windowNoTitle">true</item>
</style>
For AppCompat
, following solution worked for me: 对于
AppCompat
,以下解决方案对我AppCompat
:
Add new theme style with no action bar in your styles.xml
and set parent="Theme.AppCompat.NoActionBar"
. 在
styles.xml
添加没有动作栏的新主题样式,并设置parent="Theme.AppCompat.NoActionBar"
。
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimary</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowBackground">@color/colorPrimary</item>
</style>
Now implement the same theme style to your splash screen activity in androidManifest.xml
现在,在
androidManifest.xml
实现与初始屏幕活动相同的主题样式
<activity
android:name=".ActivityName"
android:theme="@style/SplashTheme"> // apply splash them here
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Here is result: 结果如下:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.