【发布时间】:2016-07-13 08:47:58
【问题描述】:
我是 Android 开发新手,我想在我的应用程序中隐藏标题栏。
我尽我所能隐藏该栏,但应用程序要么停止,要么没有任何改变。
我在虚拟设备上运行了演示。这是一部 5.4 英寸 FWVGA 手机。
我也尝试添加
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen"
或
android:theme="@android:style/Theme.NoTitleBar(.Fullscreen)"
但不幸的是,应用程序停止了。
请告诉我如何隐藏标题栏。
这是我的 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>
【问题讨论】:
-
您是否正在使用 AppCompat 活动,因为您需要 AppCompat 主题。使用这个 Theme.AppCompat.Light.NoActionBar