android 自定义title
16lz
2021-01-25
效果如下:
自定义标题栏的过程如下:
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为自己标题栏的布局
2.自定义标题栏样式,即:设置titlebar.xml:
<?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
2.http://guangqiang.iteye.com/blog/1116994
3.http://blog.sina.com.cn/s/blog_684a1d160100umuq.html
更多相关文章
- 一款常用的 Squid 日志分析工具
- GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
- RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
- Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
- Android(安卓)启动画面跳转和去掉标题栏
- android studio引用java8后编译报错解决方法
- MTK 平台支持设备树的添加流程
- android 添加视频、图片、录音上传(一)
- android 通知栏背景颜色跟随app导航栏背景颜色变化而变化