Android笔记之修改标题栏

一、修改标题栏样式

1、在Activity添加,顺序不能变:

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

2、新建layout/title_bar.xml来定义标题栏的控件布局

<?xml version="1.0" encoding="utf-8"?>      
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      
    android:orientation="horizontal"   
    android:layout_width="fill_parent"    
    android:layout_height="fill_parent"> 
     
    <ImageButton 
        android:id="@+id/imageButton1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:background="#00000000"         
        android:layout_centerVertical="true" 
        android:layout_alignParentLeft="true" 
        android:src="@drawable/back" /> 
 
    <TextView  
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerInParent="true" 
        android:text="标题栏" /> 
           
</RelativeLayout> 

3、为标题栏新建一个style:在value/style.xml添加,名为test

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

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

(1)<style name="CustomWindowTitleBackground"> 设置标题栏背景

(2)<item name="android:windowTitleSize">20dp</item>定义标题栏高度

4,AndroidManifest.xml相应Activity添加  android:theme="@style/test"

原文地址:https://www.cnblogs.com/xingyyy/p/3274582.html