先看代码,在主界面重载函数里写以下代码,注意requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);要在setContentView(R.layout.new_customer);前面。R.layout.new_customer是主界面,R.layout.title_new是自定义标题界面
public class New_customer extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {// TODO 自动生成的方法存根
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.new_customer);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_new);
}
}
接着贴xml代码:
new_customer.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editText_CarNum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入车牌号"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText_CarType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText_CarNum"
android:layout_marginTop="15dp"
android:hint="请输入车型"
android:ems="10" />
<EditText
android:id="@+id/editText_PersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText_CarType"
android:layout_marginTop="22dp"
android:hint="请输入车主名字"
android:ems="10" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText_PersonName"
android:layout_centerHorizontal="true"
android:layout_marginTop="67dp"
android:src="@drawable/qrcode" />
</RelativeLayout>
title_new.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/button_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:text="返回" />
<Button
android:id="@+id/button_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="确定" />
</RelativeLayout>
其他人的博客到此就为止了,但是我发现会报错,后来我知道原来是AndroidManifest.xml里android:theme="@style/AppTheme" >的问题,这里系统默认的主题不能实现自定义标题栏,所以得自己定义一个style,于是在value文件夹下的styles.xml里加上
<style name="title_bg" parent="android:Theme">
<item name="android:windowTitleSize">50dp</item>
<item name="android:windowTitleBackgroundStyle">@style/Titleground</item>
</style>
<style name="Titleground">
<item name="android:background">#ff8f26</item>
</style>
然后在AndroidManifest.xml里设置android:theme="@style/title_bg" >
这样一个自定义的标题栏就做好了。
有图有真相:
之前的小机器人不见了,取而代之的是我在标题栏设置了两个按钮,下面的内容有一个图片,测试代码的可以吧imageView去掉,图片就会消失,不然你需要导入一个同名图片才能运行。这是我的第一篇博客,望多多点赞哦。