新建Activity的时候可以继承父类AppCompatActivity和Activity
但是AppCompatActivity默认是添加标题栏的,所以如果需要自定义标题栏可以设置一下
1.想要这些活动没有标题栏,必须在manifest.xml文件中设置Application的主题为:
android:theme="@android:style/Theme.NoTitleBar"
2.AppCompatActivity这个非要标题栏才不会出错,那么就需要在oncreate()中设置它是否显示标题栏
getSupportActionBar().hide();
此时manifest.xml就不需要上一步的修改了
3.自定义标题栏
在布局文件中使用自定义组件设置标题栏
这是我写的一个简单的标题栏
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:background="@color/colorPrimary"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="30dp">
<TextView
android:id="@+id/barText"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:layout_centerVertical="true"
android:textSize="28sp"
android:layout_marginLeft="5dp"
android:text="首页"
/>
<ImageView
android:id="@+id/barAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_menu_add"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"
/>
<ImageView
android:id="@+id/barSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_menu_search"
android:layout_toLeftOf="@+id/barAdd"
android:layout_marginRight="20dp"
android:layout_centerVertical="true"
/>
</RelativeLayout>
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</LinearLayout>
最后贴一下出错的处理方案:
https://blog.csdn.net/jason0539/article/details/12979005
https://blog.csdn.net/wzhidev/article/details/64129732
还有一个设计的参考: