效果如下:
自定义标题栏的过程如下:
1.通过 requestWindowFeature来设置标题栏
2.自定义标题栏样式
3.修改默认标题栏主题
具体过程如下:
1.在oncreate方法中操作
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main); //软件activity的布局
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar); //titlebar为自己标题栏的布局
setContentView(R.layout.main); //软件activity的布局
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar); //titlebar为自己标题栏的布局
2.自定义标题栏样式,即:设置titlebar.xml:
扫描二维码关注公众号,回复:
691973 查看本文章
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <ImageView android:layout_width="wrap_content" android:layout_alignParentLeft="true" android:layout_height="wrap_content" android:padding="8dp" android:src="@drawable/icon1" /> <TextView android:layout_width="wrap_content" android:layout_centerHorizontal ="true" android:layout_height="wrap_content" android:padding="8dp" android:textSize="20sp" android:text="收藏夹" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:padding="8dp" android:src="@drawable/icon2" /> </RelativeLayout>
3.此时可以在一定程度上定制标题栏, 不过, 这里无法改变标题栏的高度和背景(背景设置之后会在两端有两个非常难看的边框),原因是这样的. 直接像上述代码那样添加title仅仅是把一个子界面添加到原有的title上的, 并没有改变原来的属性, 比如 标题栏大小, 标题栏背景. 这些需要在theme 主题里面定义.。因此需要添加一个新的styles文件,在vaules文件夹下新增一个styles.xml文件如下:
<?xml version="1.0" encoding="utf-8"?> <resources xmlns:android="http://schemas.android.com/apk/res/android"> <style name="CustomWindowTitleBackground"> <item name="android:background">#003300</item> </style> <style name="title" parent="android:Theme"> <item name="android:windowTitleSize">50dp</item> <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item> </style> </resources>
同时要在androidMainfeast.xml文件中做相应的修改:
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".MainActivity" android:label="@string/app_name" android:theme = "@style/title"//修改为新的主题 > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
此致完成一个自定义的title。
参考文献:
1. http://www.blogjava.net/Green-nut/articles/332617.html