Android自定义标题栏
2017-02-15 21:00
375 查看
创建一个Android工程,标题栏默认显示的是工程名,下面介绍如何自定义标题栏,我创建工程的MainActivity继承AppCompatActivity,效果图如下
![](https://img-blog.csdn.net/20170212111208116)
首先在AndroidManifest.xml文件中把默认的标题隐藏,设置
[java] view
plain copy
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nii.orcale" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
再创建一个标题的布局文件,就叫做title_toolbar.xml,内容如下
[java] view
plain copy
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_titlebar"
android:layout_width="match_parent"
android:layout_height="52dp"
android:background="#ed4255">
<TextView
android:id="@+id/text_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:gravity="center_horizontal|center"
android:singleLine="true"
android:text="@string/title"
android:textColor="#ffffffff"
android:textSize="20dp"/>
</RelativeLayout>
最后把这个布局加入到主的布局文件中
[html] view
plain copy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.nii.orcale.MainActivity">
<include layout="@layout/title_toolbar"></include>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
首先在AndroidManifest.xml文件中把默认的标题隐藏,设置
android:theme="@style/Theme.AppCompat.Light.NoActionBar" >
[java] view
plain copy
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nii.orcale" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
再创建一个标题的布局文件,就叫做title_toolbar.xml,内容如下
[java] view
plain copy
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_titlebar"
android:layout_width="match_parent"
android:layout_height="52dp"
android:background="#ed4255">
<TextView
android:id="@+id/text_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:gravity="center_horizontal|center"
android:singleLine="true"
android:text="@string/title"
android:textColor="#ffffffff"
android:textSize="20dp"/>
</RelativeLayout>
最后把这个布局加入到主的布局文件中
<include layout="@layout/title_toolbar"></include>
[html] view
plain copy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.nii.orcale.MainActivity">
<include layout="@layout/title_toolbar"></include>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
相关文章推荐
- 解决android自定义标题栏充满的问题
- android 修改窗体标题的字体式样和背景图(自定义标题栏)
- Android 自定义标题栏
- Android中级进阶三 自定义Android标题栏
- Android自定义标题栏
- android学习——自定义标题栏titlebar
- Android中自定义标题栏(二)
- 自定义Android标题栏修改TitleBar的布局
- Android自定义标题栏
- android之自定义标题栏
- 解决android自定义标题栏充满的问题
- 自定义android标题栏
- Android 应用程序窗体显示状态操作(显示隐藏标题栏或自定义窗体大小)
- Android 自定义标题栏
- android实现自定义标题栏
- android自定义标题栏progressBar
- Android--自定义标题栏之显示网页加载进度
- Android自定义标题栏:显示网页加载进度
- 自定义android标题栏TitleBar
- Android中自定义标题栏(一)