`
aliusa
  • 浏览: 82534 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

添加自定义风格的titleBar栏

阅读更多
1.自定义一个title layout
  title.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#454545"
    android:paddingLeft="0px">
    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="0px"
        android:text="@string/applicationTile" />

</LinearLayout>


2. 在activity的oncreate函数中添加如下代码,注意顺序不能颠倒
   
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
   setContentView(R.layout.tietohome);
   getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
 

  这样虽然可以在一定程度上定制标题栏, 不过, 这里无法改变标题栏的高度和背景(背景设置之后会在两端有两个非常难看的边框). 据说, 原因是android 固有的.

这里有修改方法:
  原理是这样的. 直接像上述代码那样添加title仅仅是把一个子界面添加到原有的title上的, 并没有改变原来的属性, 比如 标题栏大小, 标题栏背景. 这些需要在theme 主题里面定义.
  因此先定义一个style,
  若修改背景请修改android:windowTitleBackgroundStyle
  若修改标题栏高度,请修改android:windowTitleSize

例子:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">


<style name="CustomWindowTitleBackground">
<item name="android:background">#565656</item>
</style>


<style name="test" parent="android:Theme">
<item name="android:windowTitleSize">50dp</item>
<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
</style>
</resources>


在程序的android manifest.xml中对应activity中添加属性 
android:theme = "@style/test"  
就可以了

     
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics