自定义Android 标题栏TitleBar布局

方法一、
在res/value 里建一XML文档,取名为mytitlebar,内容如下:

<?xml version="1.0" encoding="utf-8"?><resources xmlns:android="http://schemas.android.com/apk/res/android"><style name="myTitleBg">       <item name="android:background">#556B2F</item></style><style name="myTheme" parent="android:Theme">     <item name="android:windowNoTitle">false</item>     <item name="android:windowTitleSize">50dp</item>     <item name="android:windowTitleBackgroundStyle">@style/myTitleBg</item></style></resources>


其中“<item name="android:windowNoTitle">false</item>”一句如果值为true,将没有标题栏,如果要显示标题栏,这句可不写。下一句是标题大小,接下来是背景颜色。
在程序的android manifest.xml中对应activity(即要自定义TitleBar的那个Activity)中添加属性 android:theme = "@style/myTheme" 就可以了

方法二、(带按钮的标题栏)

建一layout 名为mytitlebar.xml内容如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="wrap_content">         <Button android:id="@+id/pause"           android:layout_width="wrap_content"           android:layout_height="30px"            />   </LinearLayout>


就一个简单的 button,可以自己加其它的元素。

然后在java代码的onCreate中加入如下,顺序不能乱
super.onCreate(savedInstanceState);                        // 注意顺序        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);                         // 注意顺序           setContentView(R.layout.main);                            // 注意顺序        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.mytitlebar);       // 注意顺序   

更多相关文章

  1. android Intent API8
  2. 介绍一下android的各种权限。 代码如下: Html代码 <manifestxmlns
  3. Android(安卓)自定义渐变背景
  4. android wifi状态机原理
  5. android webView与js交互
  6. Android内嵌H5(2)
  7. Android开发由eclipse转Android(安卓)Studio中一些常见出错问题
  8. android摄像头采集和预览-第二种方法
  9. Android(安卓)Studio 错误 Duplicate files copied in APK META-

随机推荐

  1. Android 坑
  2. Android Permissions - Protection Level
  3. android 学习电子书——下载
  4. android调用Google搜索框
  5. Ubuntu 12.10 搭建 Eclipse Android(安卓
  6. android 3.0编译环境需要的所有组件
  7. Android文件保存和读取
  8. android 中添加 admob 广告
  9. android用代码实现圆角背景
  10. Android Google App