博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android自定义标题栏
阅读量:2305 次
发布时间:2019-05-09

本文共 1566 字,大约阅读时间需要 5 分钟。

Android程序中默认标题栏是灰色背景,内容是工程名字,类似下图:

修改了标题栏布局,效果如下:

 

说一下具体步骤。首先,修改标题栏的宽度和背景,在strings.xml中添加:

<style name="CustomWindowTitleBackground"> 

    <item name="android:background">@drawable/title_bg</item> 
</style>

<style name="test" parent="android:Theme"> 

    <item name="android:windowTitleSize">40dp</item> 
    <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item> 
</style>

然后修改AndroidMainfest.xml文件:

<activity android:name=".MainActivity" 

                android:theme="@style/test"> 
          <intent-filter> 
              <action android:name="android.intent.action.MAIN" /> 
              <category android:name="android.intent.category.LAUNCHER" /> 
          </intent-filter> 
      </activity>

红色部分为加载Activity的theme。

接着修改MainActivity:

public void onCreate(Bundle savedInstanceState) { 

       super.onCreate(savedInstanceState); 
       requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); //声明使用自定义标题 
       setContentView(R.layout.main); 
       getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);//自定义布局赋值 
   }

注意红色部分标记的3行代码,顺序不能颠倒。

最后在layout文件夹中添加title.xml文件,内容如下:

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android=" 
    android:orientation="horizontal" android:layout_width="fill_parent" 
    android:layout_height="fill_parent">

    <ImageView android:layout_width="wrap_content" 

        android:layout_centerVertical=’true’ 
        android:layout_height="wrap_content" 
        android:src="@drawable/ic_launcher_email" /> 
    <TextView android:layout_width="wrap_content" 
        android:layout_centerInParent="true" 
        android:layout_height="wrap_content" 
        android:text="自定义标题栏" />

</RelativeLayout>

原文:

转载于:https://www.cnblogs.com/HelloGuang/p/3680286.html

你可能感兴趣的文章
嵌入式QT软键盘
查看>>
docker运行eureka服务注册中心
查看>>
Raid详解
查看>>
MySQL5.7重启MySQL服务时报错解决方案
查看>>
Mycat简单实现读写分离与分库分表
查看>>
Swift Socket 实例 两份资料不错
查看>>
pthread之工作模式
查看>>
路由器DSCP target
查看>>
java.util.ConcurrentModificationException
查看>>
jquery拿到HtmlObject类型
查看>>
ddpush 学习之路 3 StringUtil.java
查看>>
Linux【oh-my-zsh】ls命令无法tab键跳转文件文件夹
查看>>
杂谈:项目管理的是与非
查看>>
Java1.0/1.1中遗留的集合
查看>>
Linux平台超级好用服务器远程管理工具webmin的安裝
查看>>
去掉Sublime Text 3烦人的更新新版本提醒
查看>>
启动vsftpd的问题---500 OOPS
查看>>
ubuntu lamp的部署
查看>>
CAS简单实例
查看>>
CAS结合openldap
查看>>