android 新建项目中去掉标题栏

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并把相应提示报错处理一下就可以。

具体代码如下:

 package com.example.abc;

 import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build; public class MainActivity extends FragmentActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
} // @Override
// public boolean onCreateOptionsMenu(Menu menu) {
//
// // Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.main, menu);
// return true;
// }
//
// @Override
// public boolean onOptionsItemSelected(MenuItem item) {
// // Handle action bar item clicks here. The action bar will
// // automatically handle clicks on the Home/Up button, so long
// // as you specify a parent activity in AndroidManifest.xml.
// int id = item.getItemId();
// if (id == R.id.action_settings) {
// return true;
// }
// return super.onOptionsItemSelected(item);
// } /**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() {
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
} }

运行正确:

android 新建项目中去掉标题栏

上一篇:Spring是如何校验XML的


下一篇:ios7 ios8导航栏透明