【Android UI】自定义带按钮的标题栏
来源:互联网 发布:电脑fm软件 编辑:程序博客网 时间:2024/05/22 10:05
自定义标题栏在很多的Android app中很常见,可以说是一种很有用的UI设计方法。自
己也本着学习的态度,经过一番各种坑,终于实现了,现总结如下:
一:大致流程
1. 对指定的android activity设置自定义主题风格,其中自定义主题风格是关键
在android 4.0以上版本中如果使用Theme.Holo或者Theme.Light等,程序会
一直报错误-you cannot combine custom title with other feature titles
2. 在对应的Activity中加入代码
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
- setContentView(R.layout.main);
- getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.mycustomtitle);
3. 在styles.xml使用如下的自定义主题,发现只有使用这个默认主题才不出第一步的
错误,真是各种坑啊!
- <resources>
- <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>
- </resources>
二:测试MainActivity源代码
- package com.gloomyfish.titledemo;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Window;
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
- setContentView(R.layout.main);
- getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.mycustomtitle);
- }
- }
mycustomtitle.xml的内容
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_gravity="fill_horizontal"
- android:orientation="horizontal"
- android:layout_height="fill_parent" >
- <Button android:id="@+id/header_left_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_marginLeft="5dp"
- android:layout_centerVertical="true"
- android:text="返回"
- android:textColor="#000000"/>
- <TextView android:id="@+id/header_text"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_toRightOf="@+id/header_left_btn"
- android:layout_toLeftOf="@+id/header_right_btn"
- android:text="My Title Bar"
- android:textSize="20sp"
- android:textStyle="bold"
- android:textColor="#FFFFFF"
- android:gravity="center"
- android:layout_centerHorizontal="true"
- android:layout_centerVertical="true"
- android:singleLine="true" />
- <Button android:id="@+id/header_right_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_marginRight="5dp"
- android:layout_centerVertical="true"
- android:text="图片"
- android:textColor="#000000"/>
- </RelativeLayout>
- android:theme="@style/MyTheme"
最终效果如下:
实现这个自定义标题栏的时候,看到stackoverflow上面说
如果使用Theme.Holo一定要换成Theme.Holo.NoActionBar主题
可以我换了以后发现,还是各种坑,一直不出效果。
所以我推荐一定要使用android:theme
0 0
- 【Android UI】自定义带按钮的标题栏
- 【Android UI】自定义带按钮的标题栏
- 【Android UI】自定义带按钮的标题栏
- 【Android UI】自定义带按钮的标题栏
- android带返回按钮的自定义标题栏
- android带返回按钮的自定义标题栏
- Android自定义带按钮标题栏
- android带返回按钮的自定义标题栏布局文件详解
- React Native自定义带返回按钮的标题栏组件
- Android 完全自定义对话框的实现(标题栏+EditText+双按钮)
- 自定义Android带图片的按钮
- 自定义Android带图片的按钮
- 自定义Android带图片的按钮
- 自定义Android带图片的按钮
- android 自定义带关闭按钮的dialog
- Android UI之ImageButton(带图标的按钮)
- 玩转Android---UI篇---ImageButton(带图标的按钮)
- Android自定义视频录制时的带进度条的按钮
- Node.js Buffer
- 非IE冒泡与捕获
- apache配置文件:http.conf配置详解
- sap常用bapi
- 01_数据结构和算法绪论
- 【Android UI】自定义带按钮的标题栏
- ajax动态加载json数据并解析
- Android资源管理框架(Asset Manager)简要介绍和学习计划
- Notepad++搭配MinGW编译运行C/C++程序
- linux环境下memcache的安装
- Error:A problem was found with the configuration of task ':app:packageDebug'. > File 'F:\androidstud
- Xstream解析:xml
- android开发去掉顶部标题栏的方法
- 机器学习框架简介