android 自定标题栏的应用

本文介绍了如何在Android应用中自定义标题栏,通过设置窗口布局、引用自定义标题XML文件,以及设置不同部分的文本内容,实现了标题栏的个性化展示。

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

一、先上个图吧,我程序上的截图


二、上程序

1、

// 设置窗口的布局
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);// 自定义标题
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.custom_title);//自定义标题栏的xml文件
//从程序看放在主activity中,而且上面三步顺序不能随便写,就按上面顺就可以了。

下面来上R.layout.custom_title,,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="fill_parent"
    android:background="@drawable/share_custom_title"
     >


    <ImageView//这个控件我放那个黄色小图标的。
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginLeft="3dp"
        android:layout_marginTop="2dp"
        android:background="@drawable/app_icon" >
    </ImageView>


    <TextView
        android:id="@+id/title_left_text"
        style="?android:attr/windowTitleStyle"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="25dp"
        android:layout_weight="1"
        android:background="#00000000"
        android:ellipsize="end"
        android:singleLine="true" />


    <TextView//这个就是中间那个显示温度的。
        android:id="@+id/title_centre_text" 
        style="?android:attr/windowTitleStyle"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="130dp"
        android:layout_weight="1"
        android:background="#00000000"
        android:ellipsize="end"
        android:singleLine="true" />


    <TextView //这个就是用来显示连接状态的。
        android:id="@+id/title_right_text"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_weight="1"
        android:background="#00000000"
        android:ellipsize="end"
        android:singleLine="true"
        android:textColor="#ffffff" 
        android:layout_marginRight="2dp"
        />


</RelativeLayout>

// 设置自定义标题ID引用
TitleLeft = (TextView) findViewById(R.id.title_left_text);
TitleLeft.setText(R.string.app_name);
TitleCentre = (TextView) findViewById(R.id.title_centre_text);
TitleRight = (TextView) findViewById(R.id.title_right_text);

结语:不知道忘掉什么没写没有,有了以上步骤就可以实现自定义标题了。


源码下载:http://www.yxwojia.com/sort-3105990-1.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值