How to add custom title bar to android application
来源:互联网 发布:音速启动是什么软件 编辑:程序博客网 时间:2024/04/30 14:30
Article from : http://help.discretelogix.com/android/how-to-add-custom-title-bar-to-android-application.htm
There are multiple ways to customize title bar in android application. Today, I am going to show a way in which we’ll completely change the looks of title bar and add extra views to it.
Step 1: Create layout file
First of all create an xml file for your title bar layout.
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="44dp" android:orientation="horizontal" android:gravity="center_vertical" android:background="#FFFFFF" > <ImageViewandroid:id="@+id/dxLogo"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="left" android:src="@drawable/dx_icon"android:layout_marginRight="10dip" /> <TextView android:id="@+id/titleHeading"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/app_name"style="@style/titleBarHeading" /></LinearLayout>
In this layout, I have included an image that is used to display a logo and then a text view to display activity heading.
Step 2: Create application theme file
In order to set a custom title bar properly, you have to create a new theme for your application. If you don’t specify padding and background color, your custom title bar won’t fill screen width.
<?xml version="1.0" encoding="utf-8"?><resources> <style name="appTheme" parent="@android:style/Theme.Black"> <item name="android:textColor">#444444</item> <item name="android:windowTitleBackgroundStyle">@style/titleBarBackground</item> <item name="android:windowTitleSize">44dip</item> </style> <style name="titleBarHeading" parent="@android:style/TextAppearance"> <item name="android:textSize">17sp</item> <item name="android:textStyle">bold</item> <item name="android:textColor">#444444</item> </style> <style name="titleBarBackground"> <item name="android:background">@android:color/transparent</item> <item name="android:padding">0px</item> </style> </resources>
In this theme file, I have defined styles for title bar itself as well as the text view that we defined in title bar layout file. Adding style to change padding of title bar is necessary otherwise s small border will be visible.
After that, change you manifest file to specify application theme.
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/appTheme" >
Step 3: Create a custom activity class
Create a custom activity class which will act as parent to all your activities. OnCreate of your parent activity, set the layout of title bar to the one you have just created.
public class CashBookActivity extends Activity { @Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.main); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title); }}
The code above assume that the XML file containing your title layout is window_title.xml. Now extend all your activities from this parent class.
public class FormActivity extends CashBookActivity {@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState); setContentView(R.layout.form);TextView tvHeading = (TextView) findViewById(R.id.titleHeading);tvHeading.setText("Add New Transaction");}}
Save and run your application, you should see your custom title bar.
Note: Do not extend your application’s custom theme from the default android themes having "NoTitleBar"
0 0
- How to add custom title bar to android application
- How to add custom scripts
- How to Create Custom Window Title in Android
- how to set custom navigator title
- How to add a Custom Pull to Refresh in your iOS Application
- How to add a custom build step to MSBuild Projects
- How to add custom validator to paper-input?
- Android Custom Dialog错误Unable to add window -- token null is not for an application
- Android Custom Dialog错误Unable to add window -- token null is not for an application
- Android Custom Dialog错误Unable to add window -- token null is not for an application
- Add a custom tool to the web mapping application
- How to Add Virtual Keys on Status Bar
- How To Add Search Bar in Table View
- How to close Android application?
- How to add application to right click menu in Windows
- How to Add Core Data to an existing Utility Application
- How To Add Breakpad To Your Linux Application
- HOW TO Custom DSDT
- 快速排序算法
- 华为编程大赛 每日一练(二)
- android的消息处理机制(图+源码分析)——Looper,Handler,Message+10
- Permutation Sequence - 20 mins
- HDU 3410 && POJ 3776 Passing the Message 单调队列
- How to add custom title bar to android application
- Windows 7 or 8 Master Boot Record (MBR)
- 【操作系统】线程实现方式(内核级线程、用户级线程)
- UVa 11401 11401 - Triangle Counting
- oracle 系统默认的用户及密码
- Linux 系统挂载数据盘教程
- java Applet绘图。。。
- 初观设计模式
- MySQL+Tomcat+J2EE中文乱码问题的UTF-8的解决方案