(之前在这个坑待太久,所以在此总结分享。)
一、标题栏简介
自定义的title控件是UI常用的一个设计手法,最简单的例子如图(找了个丑一点的)
二、使用过程
1.(注意:那个标题栏的布局文件和那个所在的activity的布局文件是不同的!!!)先建一个你想要的标题栏的布局文件,弄成你想要的标题的布局代码,只是上面的标题栏,所以属性、大小要注意一下
2.你需要一个自定义的主题,在styles那里建立建立一个你想要的apptheme
3.记得在对应的activity里面注册你的主题(styles)最好使用默认主题,不然容易出一堆error
4.在对应的activity的java文件里面码上引用的代码
三、详细流程
1.标题栏布局文件tips_title,这个只是标题栏的title布局,不是activity的布局,activity的布局要另外写
<?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="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="温馨提示"
android:textSize="30sp"
android:textAlignment="center"
android:id="@+id/tips" />
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:id="@+id/header_text"
android:layout_toRightOf="@+id/tips" />
<Button
android:id="@+id/feedback"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="有错误?"
android:background="#6495ED"
android:textSize="25sp"
android:layout_toRightOf="@id/header_text" />
</LinearLayout>
2.在资源文件的styles里面声明你要的theme
如:这个基本是默认的主题了,颜色,字体可以改,但是怕出error就别改其他的太多了
<style name="WindowTitleBackground" >
<item name="android:background">@color/blue</item>
</style>
<style name="MyTheme" parent="android:Theme">
<item name="android:windowTitleSize">60dp</item>
<item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>
</style>
3.在注册文件manifests那里的对应的activity那里声明你的主题
如: 最好是使用android:theme="@style/" 这种格式,同时还要删除ide默认生成的主题apptheme之类的,不然还是报错。
<activity android:name=".TIPS" android:theme="@style/MyTheme" />
如:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); //先声明这个activity会有一个标题栏,并且利用这个方法
setContentView(R.layout.tips);<span style="white-space:pre"> //声明改activity的布局文件
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tips_title); //声明引用我们所做的title布局文件
图在上面,有点丑 别介意。
ok,该注意的、易出错的都尽量写详细了。