android中自定义Theme以及TitleBar

1.自定义Theme。

    在res/values/styles.xml中的resources块中添加如下代码:

<style name="StatusBarBackground">
    <item name="android:background">#ff888888</item>
</style>

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

 

    在AndroidMainfest.xml文件中使用该Theme。根据自己的需要在application或者activity块中添加如下语句:

android:theme="@style/test"

 

2.自定义TitleBar。

    在代码中加载界面的地方添加如下代码:

        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);   
        setContentView(R.layout.activity_main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);

    注意3条语句的顺序。

    其中R.layout.titlebar为自己定义的布局,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:background="#333">

    <ImageView
        android:id="@+id/titleImage"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:padding="10dp"
        android:src="@drawable/ic_drawer">
    </ImageView>

    <TextView
        android:id="@+id/titletext"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:paddingLeft="6dp"
        android:paddingRight="6dp"
        android:textColor="#fff" >
    </TextView>

</LinearLayout>

 

posted on 2013-07-25 16:51  buptpatriot  阅读(281)  评论(0编辑  收藏  举报

导航