android 新建项目中去掉标题栏

时间:2021-10-17 19:27:50

1.新建new android application project

  theme选none 并打钩创建一个Blank Activity

 运行如下图所示:

android 新建项目中去掉标题栏

2。若想把标题栏去掉,更改Manifestr的MainActivity 主题样式

>styles.xml中改

开始将<style name="AppTheme" parent="AppBaseTheme">

改为:<style name="AppTheme" parent="android:Theme.Light.NoTitleBar">//无标题白色背景

去行,闪退并报错:Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

3。原因,是由于public class MainActivity extends ActionBarActivity在创建的时候继承了ActionBarActivity有标题的类,这样就会报错

更改继承FragmentActivity并把相应提示报错处理一下就可以。

具体代码如下:

 1 package com.example.abc;
 2 
 3 import android.support.v7.app.ActionBarActivity;
 4 import android.support.v7.app.ActionBar;
 5 import android.support.v4.app.Fragment;
 6 import android.support.v4.app.FragmentActivity;
 7 import android.os.Bundle;
 8 import android.view.LayoutInflater;
 9 import android.view.Menu;
10 import android.view.MenuItem;
11 import android.view.View;
12 import android.view.ViewGroup;
13 import android.os.Build;
14 
15 public class MainActivity extends FragmentActivity {
16 
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21 
22         if (savedInstanceState == null) {
23             getSupportFragmentManager().beginTransaction()
24                     .add(R.id.container, new PlaceholderFragment()).commit();
25         }
26     }
27 
28 //    @Override
29 //    public boolean onCreateOptionsMenu(Menu menu) {
30 //
31 //        // Inflate the menu; this adds items to the action bar if it is present.
32 //        getMenuInflater().inflate(R.menu.main, menu);
33 //        return true;
34 //    }
35 //
36 //    @Override
37 //    public boolean onOptionsItemSelected(MenuItem item) {
38 //        // Handle action bar item clicks here. The action bar will
39 //        // automatically handle clicks on the Home/Up button, so long
40 //        // as you specify a parent activity in AndroidManifest.xml.
41 //        int id = item.getItemId();
42 //        if (id == R.id.action_settings) {
43 //            return true;
44 //        }
45 //        return super.onOptionsItemSelected(item);
46 //    }
47 
48     /**
49      * A placeholder fragment containing a simple view.
50      */
51     public static class PlaceholderFragment extends Fragment {
52 
53         public PlaceholderFragment() {
54         }
55 
56         @Override
57         public View onCreateView(LayoutInflater inflater, ViewGroup container,
58                 Bundle savedInstanceState) {
59             View rootView = inflater.inflate(R.layout.fragment_main, container,
60                     false);
61             return rootView;
62         }
63     }
64 
65 }

运行正确:

android 新建项目中去掉标题栏