Android自定义标题栏 TItle Bar

本文详细介绍了如何在Android应用中自定义标题栏,包括修改代码、使用XML布局文件以及设置主题样式,最终实现自定义的返回和确定按钮。通过实例演示,帮助开发者轻松掌握自定义Android应用标题栏的技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先看代码,在主界面重载函数里写以下代码,注意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去掉,图片就会消失,不然你需要导入一个同名图片才能运行。这是我的第一篇博客,望多多点赞哦。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值