自定义标题栏在很多的android app中很常见,可以说是一种很有用的UI设计方法。自

己也本着学习的态度,经过一番各种坑,终于实现了,现总结如下:

一:大致流程

1.      对指定的android activity设置自定义主题风格,其中自定义主题风格是关键

在android 4.0以上版本中如果使用Theme.Holo或者Theme.Light等,程序会

一直报错误-you cannot combine custom title with other feature titles

2.      在对应的Activity中加入代码

  1. super.onCreate(savedInstanceState);
  2. requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
  3. setContentView(R.layout.main);
  4. getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.mycustomtitle);

3.      在styles.xml使用如下的自定义主题。

  1. <resources>
  2.  
  3. <style name="WindowTitleBackground" >
  4.  
  5. </style>
  6. <style name="MyTheme" parent="android:Theme">
  7. <item name="android:windowTitleSize">60dp</item>
  8. <item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>
  9. </style>
  10.  
  11. </resources>

二:测试MainActivity源代码

MainActivity.java

  1. package com.example.title_bar;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.view.Window;
  9. import android.widget.Button;
  10.  
  11. public class MainActivity extends Activity {
  12.  
  13. Button button;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
  18. setContentView(R.layout.activity_main);
  19. getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.titlebarstyle);
  20.  
  21. button=(Button) findViewById(R.id.button1);
  22.  
  23. button.setOnClickListener(new OnClickListener(){
  24.  
  25. @Override
  26. public void onClick(View v) {
  27. // TODO Auto-generated method stub
  28. Intent intent=new Intent(MainActivity.this,test_title_bar.class);
  29.  
  30. startActivity(intent);
  31. }
  32.  
  33. });
  34. }
  35.  
  36. }

test_title_bar.java

  1. package com.example.title_bar;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.Window;
  6. import android.widget.Button;
  7. import android.widget.TextView;
  8.  
  9. public class test_title_bar extends Activity{
  10.  
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. // 去标题
  15. //requestWindowFeature(Window.FEATURE_NO_TITLE);
  16. requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
  17. setContentView(R.layout.titlebartest);
  18. //设置标题为某个layout,标题样式
  19. getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebarstyle);
  20.  
  21. //设置标题栏的标题
  22. settitle("hello","我是测试页面二","world");
  23. }
  24.  
  25. private void settitle(String btn1str,String string,String btn2str) {
  26. // TODO Auto-generated method stub
  27.  
  28. Button btnback=(Button) findViewById(R.id.back);
  29. Button btnnext=(Button) findViewById(R.id.next);
  30. TextView tvtitle=(TextView) findViewById(R.id.title);
  31.  
  32. btnback.setText(btn1str);
  33. tvtitle.setText(string);
  34. btnnext.setText(btn2str);
  35. }
  36.  
  37. }

三:XML资源文件

titlebarstyle.xml的内容

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_gravity="fill_horizontal"
  4. android:orientation="horizontal"
  5. android:layout_height="fill_parent" >
  6. <Button android:id="@+id/header_left_btn"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:layout_alignParentLeft="true"
  10. android:layout_marginLeft="5dp"
  11. android:layout_centerVertical="true"
  12. android:text="back"
  13. android:textColor="#000000"/>
  14.  
  15. <TextView android:id="@+id/header_text"
  16. android:layout_width="fill_parent"
  17. android:layout_height="fill_parent"
  18. android:layout_toRightOf="@+id/header_left_btn"
  19. android:layout_toLeftOf="@+id/header_right_btn"
  20. android:text="My Title Bar"
  21. android:textSize="20sp"
  22. android:textStyle="bold"
  23. android:textColor="#FFFFFF"
  24. android:gravity="center"
  25. android:layout_centerHorizontal="true"
  26. android:layout_centerVertical="true"
  27. android:singleLine="true" />
  28.  
  29. <Button android:id="@+id/header_right_btn"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:layout_alignParentRight="true"
  33. android:layout_marginRight="5dp"
  34. android:layout_centerVertical="true"
  35. android:text="next"
  36. android:textColor="#000000"/>
  37.  
  38. </RelativeLayout>

activity_main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context="com.example.title_bar.MainActivity" >
  10.  
  11. <TextView
  12. android:id="@+id/textView1"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="我是测试页一" />
  16.  
  17. <Button
  18. android:id="@+id/button1"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_below="@+id/textView1"
  22. android:layout_marginTop="97dp"
  23. android:text="去测试页面二" />
  24.  
  25. </RelativeLayout>

titlebartest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6.  
  7. <TextView
  8. android:id="@+id/textView1"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="我是测试界面二" />
  12.  
  13. </LinearLayout>

最后别忘记在android的manifest配置文件中加上自定义的主题

  1. android:theme="@style/MyTheme"

界面一:

界面二:

【Android UI】自定义带按钮的标题栏的更多相关文章

  1. Android实现自定义带文字和图片的Button

    Android实现自定义带文字和图片的Button 在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法. 一.用系统自带的Button实现 最简单的一种办法就 ...

  2. 【Android】Android实现自定义带文字和图片的Button

    在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法. 一.用系统自带的Button实现 最简单的一种办法就是利用系统自带的Button来实现,这种方式代码量最 ...

  3. android 弹出带按钮的对话框

    package com.example.helloworld; import android.os.Bundle;import android.app.Activity;import android. ...

  4. Android UI自定义Spinner下拉框(用popuwindow实现)-转

    定义出第一个图片的布局和弹出框(一个listView)的布局,,这里就不在多说了~ListView需要自己定义一个MyspinnerAdapter~做好这些准备之后,就是弹出框的实现了~  prote ...

  5. [转]Android UI:看看Google官方自定义带旋转动画的ImageView-----RotateImageView怎么写(附 图片淡入淡出效果)

    http://blog.csdn.net/yanzi1225627/article/details/22439119 众所周知,想要让ImageView旋转的话,可以用setRotation()让其围 ...

  6. Android UI:看看Google官方自定义带旋转动画的ImageView-----RotateImageView怎么写(附 图片淡入淡...)

    众所周知,想要让ImageView旋转的话,可以用setRotation()让其围绕中心点旋转,但这个旋转是不带动画的,也就是旋转屏幕时图片噌的一下就转过去了,看不到旋转的过程,此UI体验不大好,为此 ...

  7. Android UI(五)云通讯录项目之联系人列表,带侧滑选择,带搜索框

    作者:泥沙砖瓦浆木匠网站:http://blog.csdn.net/jeffli1993个人签名:打算起手不凡写出鸿篇巨作的人,往往坚持不了完成第一章节.交流QQ群:[编程之美 365234583]h ...

  8. Android UI 绘制过程浅析(五)自定义View

    前言 这已经是Android UI 绘制过程浅析系列文章的第五篇了,不出意外的话也是最后一篇.再次声明一下,这一系列文章,是我在拜读了csdn大牛郭霖的博客文章<带你一步步深入了解View> ...

  9. Android 自定义Button按钮显示样式(正常、按下、获取焦点)

    现在的用户对APP的外观看得很重要,如果APP内所有元件都用Android默认样式写,估计下面评论里就有一堆在骂UI丑的.今天学习自定义Button按钮样式.Button样式修改的是Button的背景 ...

随机推荐

  1. 为什么360、百度、腾讯出的Mac端云盘客户端都只有同步盘?(用户量小,同步盘开发成本低,Linux下都没有客户端)

    如题,顾名思义,同步盘是用来同步的,不具备增量的功能,像这三家在Windows端出的客户端都是即有同步也有增量的. 陆续出来的,可能大家更多的是跟随策略,不得不提dropbox是这样的形式.mac电脑 ...

  2. Android零基础入门第75节:Activity状态和生命周期方法

    前面两期我们学习了Activity的创建和注册.以及启动和关闭,也学会了重写onCraete方法,这些知识在实际开发中远远不够,还需要学习了解更多. 生命周期就是一个对象从创建到销毁的过程,每一个对象 ...

  3. SOA 相关开发调试软件

    开发工具 IntelliJ IDEA:https://www.jetbrains.com/idea/ SOA调试 soapui:http://www.soapui.org/ wcfstorm:http ...

  4. Qt编程规范

    一.概述 良好的编程规范可以大幅提高一个程序的可读性.可理解性和可维护性. 本规范参考Effective C++中文版.Google C++编码规范及Qt编码风格. 二.头文件 1)      #de ...

  5. DI 容器实务建议

    整理一些有关使用 DI 容器的一些建议事项,主要的参考数据源是 Jimmy Board 的文章:Container Usage Guidelines. 1.容器设定 避免对同一个组件(DLL)重复扫描 ...

  6. IT安全军火库-转

    全球有260万信息安全专业人士,渗透测试工具是他们“安全军火库”中最常使用的装备,但直到最近,可用的渗透测试工具才丰富起来,但这也带来一个问题,挑选合适的渗透测试工具成了一件麻烦事,一个最简单的方法就 ...

  7. STL函数static void (* set_malloc_handler(void (*f)()))()与函数指针解析

    在C++ STL的SGI实现版本中,一级空间配置器class __malloc_alloc_template中有一个静态函数的实现如下: static void (*set_malloc_handle ...

  8. 【原创】ABAP根据文件路径获取文件所在目录(续)

    在上一篇文章<ABAP根据文件路径获取文件所在目录>中,我主要的思路是采用 “SPLIT dobj AT sep INTO TABLE result_tab” 句型将文件全路径按分隔符“\ ...

  9. c++ LeetCode (初级字符串篇) 九道算法例题代码详解(二)

    原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/11089327.html 已经刷了很多篇leetcode题了,不过最近在找c++的实习工作(大佬 ...

  10. Python连载10-os包函数(续)

    一.os包(接连载9) 1.函数:system() (1)用法:运行系统shell命令 (2)格式:os.system(系统命令) (3)返回值:打开一个shell或终端界面 (4)注意:一般是用su ...