Android 标题栏(TItle Bar)自定义 美化

瘦欲@ 提交于 2021-02-18 03:51:38

1. 标题栏显示图标

publicvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_LEFT_ICON);

        setContentView(R.layout.main);
    
        getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
                        android.R.drawable.icon);
        // ...
}但实际效果呢,我觉得不好看,和旁边的文字有相当距离!看看别人的图片的:
当然这个图标也可以通过自定义布局,使用ImageView来实现:<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <ImageViewandroid:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:src="@drawable/icon"/>
    <TextViewandroid:id="@+id/text"    
            android:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:layout_alignParentLeft="true"    
            android:text="文本"/>    
</LinearLayout>效果图:
2.自定义布局看看我自定义的标题栏:布局代码(titlebar.xml)<?xmlversion="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"
>
        <TextView
                android:text="@string/app_name"
                android:textColor="#000"
                android:paddingRight="3.0dip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        <TextView
                android:text="@string/battery_text"
                android:textColor="#000"
                android:paddingRight="3.0dip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        <TextView
                android:id="@+id/battery_text"
                android:textColor="#00f"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
</LinearLayout>

Java代码:publicvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

        setContentView(R.layout.main);

        //自定义标题栏
        mWindow = getWindow();
        mWindow.setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.titlebar);

        mBatteryText = (TextView)findViewById(R.id.battery_text);

        mBatteryInforeceiver = new BroadcastReceiver(){

             @Override
             publicvoid onReceive(Context context, Intent intent) {
                     int level = intent.getIntExtra("level", 0);
                     int scale = intent.getIntExtra("scale", 1);
                     mBatteryText.setText(String.valueOf((int)(level*100/scale))+"%");
             }
            
        };
}你还可以添加其他控件,而这些控件的获取和事件响应都是直接在activity里面完成。
3. 设置标题栏的背景色和高度虽然我们可以通过自定义布局文件在标题栏加入一些控件,但是仍然不能改变标题栏的高度、背景色,要想达到这个目的,只能使用theme(主题)。\res\values\style.xml:<?xmlversion="1.0"encoding="utf-8"?>
<resources>
        <stylename="CustomWindowTitleBackground">
                <itemname="android:background">#47B2FF</item>
        </style>

        <stylename="activityTitlebar"parent="android:Theme">
                <itemname="android:windowTitleSize">34dp</item> <!-- 高度 -->
                <itemname="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>    <!-- 背景色,需要调用前面的颜色设置 -->
        </style>
</resources>

窗体显示状态操作(requestWindowFeature()的应用)
首先介绍一个重要方法那就是requestWindowFeature(featrueId),它的功能是启用窗体的扩展特性。参数是Window类中定义的常量。一、枚举常量1.DEFAULT_FEATURES:系统默认状态,一般不需要指定2.FEATURE_CONTEXT_MENU:启用ContextMenu,默认该项已启用,一般无需指定3.FEATURE_CUSTOM_TITLE:自定义标题。当需要自定义标题时必须指定。如:标题是一个按钮时4.FEATURE_INDETERMINATE_PROGRESS:不确定的进度5.FEATURE_LEFT_ICON:标题栏左侧的图标6.FEATURE_NO_TITLE:没标题7.FEATURE_OPTIONS_PANEL:启用“选项面板”功能,默认已启用。8.FEATURE_PROGRESS:进度指示器功能9.FEATURE_RIGHT_ICON:标题栏右侧的图标
对于默认启用的和前面有介绍的就略去不提了。我们说比较常用的FEATURE_INDETERMINATE_PROGRESS和FEATURE_NO_TITLE。
FEATURE_INDETERMINATE_PROGRESS:表示一个进程正在运行progress.xml<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ProgressBarandroid:id="@+id/progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"    
            android:layout_gravity="center_vertical"
            style="?android:attr/progressBarStyleSmallTitle">
    </ProgressBar>
</LinearLayout>
Java代码publicvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.main);

        getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS, R.layout.progress);
        setProgressBarIndeterminateVisibility(true); //适当时候set false来隐藏

        //...
}
标题进度条显示
FEATURE_NO_TITLE 就是不显示标题栏,某些时候全屏需要,但全屏不等于不显示标题栏,我尝试显示标题栏的同时全屏来去掉系统的状态栏:Java代码publicvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

        setContentView(R.layout.main);

        //自定义标题栏
        mWindow = getWindow();
        mWindow.setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.titlebar);

        /* full screen */
        mWindow.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                        WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // ...
}所以真正实现全屏的是后面的那句话!
效果图
***********本文部分内容摘录于《Android 应用程序窗体显示状态操作(requestWindowFeature()的应用)》实例如下:Java代码  
  1. package com.easyway.titlebar.androids;   
  2.   
  3. import android.app.Activity;   
  4. import android.content.BroadcastReceiver;   
  5. import android.content.Context;   
  6. import android.content.Intent;   
  7. import android.os.Bundle;   
  8. import android.view.Window;   
  9. import android.widget.TextView;   
  10.   
  11. public class CustomTitleBarActivity extends Activity {   
  12.     /** Called when the activity is first created. */  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {   
  15.         super.onCreate(savedInstanceState);   
  16.       requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);    
  17.     
  18.       setContentView(R.layout.main);   
  19.       //自定义标题栏   
  20.       Window mWindow = getWindow();   
  21.       mWindow.setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.titlebar);   
  22.       final TextView mBatteryText = (TextView)findViewById(R.id.battery_percent);   
  23.   
  24.       BroadcastReceiver mBatteryInforeceiver = new BroadcastReceiver(){   
  25.            @Override  
  26.            public void onReceive(Context context, Intent intent) {   
  27.                    int level = intent.getIntExtra("level", 0);   
  28.                    int scale = intent.getIntExtra("scale", 1);   
  29.                    mBatteryText.setText("Android开发:"+String.valueOf((int)(level*100/scale))+"%");   
  30.            }   
  31.               
  32.       };   
  33.   
  34.     }   
  35. }  
package com.easyway.titlebar.androids;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.view.Window;import android.widget.TextView;public class CustomTitleBarActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.main); //自定义标题栏 Window mWindow = getWindow(); mWindow.setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.titlebar); final TextView mBatteryText = (TextView)findViewById(R.id.battery_percent); BroadcastReceiver mBatteryInforeceiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { int level = intent.getIntExtra("level", 0); int scale = intent.getIntExtra("scale", 1); mBatteryText.setText("Android开发:"+String.valueOf((int)(level*100/scale))+"%"); } }; }} titlebar.xml配置Xml代码  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.         xmlns:android="http://schemas.android.com/apk/res/android"  
  4.         android:orientation="horizontal"  
  5.         android:layout_width="fill_parent"  
  6.         android:layout_height="wrap_content"  
  7. >  
  8.         <TextView  
  9.                 android:text="@string/app_name"  
  10.                 android:textColor="#000"  
  11.                 android:paddingRight="3.0dip"  
  12.                 android:layout_width="wrap_content"  
  13.                 android:layout_height="wrap_content"  
  14.                 />  
  15.         <TextView  
  16.                 android:text="@string/battery_text"  
  17.                 android:textColor="#000"  
  18.                 android:paddingRight="3.0dip"  
  19.                 android:layout_width="wrap_content"  
  20.                 android:layout_height="wrap_content"/>  
  21.         <TextView  
  22.                 android:id="@+id/battery_percent"  
  23.                 android:textColor="#00f"  
  24.                 android:layout_width="wrap_content"  
  25.                 android:text="55%"  
  26.                 android:layout_height="wrap_content"/>  
  27. </LinearLayout>  
<?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"> <TextView android:text="@string/app_name" android:textColor="#000" android:paddingRight="3.0dip" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="@string/battery_text" android:textColor="#000" android:paddingRight="3.0dip" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/battery_percent" android:textColor="#00f" android:layout_width="wrap_content" android:text="55%" android:layout_height="wrap_content"/></LinearLayout> Xml代码  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.easyway.titlebar.androids"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="14" />  
  8.   
  9.     <application  
  10.         android:icon="@drawable/ic_launcher"  
  11.         android:label="@string/app_name" >  
  12.         <activity  
  13.             android:label="@string/app_name"  
  14.             android:theme="@style/activityTitlebar"  
  15.             android:name=".CustomTitleBarActivity" >  
  16.             <intent-filter >  
  17.                 <action android:name="android.intent.action.MAIN" />  
  18.   
  19.                 <category android:name="android.intent.category.LAUNCHER" />  
  20.             </intent-filter>  
  21.         </activity>  
  22.     </application>  
  23.   
  24. </manifest> 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!