android带返回按钮的自定义标题栏

定义标题栏样式

 

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <style name="TitleBarBackground">  
  5.         <item name="android:background">@color/style_blue</item>  
  6.     </style>  
  7.             
  8.      <style name="MyCustomTheme" parent="android:Theme">       
  9.          <item name="android:windowTitleBackgroundStyle">@style/ TitleBarBackground</item>  
  10.          <item name="android:windowTitleSize">50dp</item>  
  11.      </style>   
  12.   
  13. </resources>  


 

定义标题栏布局 custom_title.xml

  标题栏布局中可以根据自身需求定义。

[html]  view plain copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:id="@+id/screen"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical">  
  6.   
  7.         <TextView  
  8.         android:id="@+id/head_center_text"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_centerInParent="true"  
  12.         android:text=""   
  13.         android:textColor="#FFFFFF"  
  14.         />  
  15.       
  16.     <TextView  
  17.         android:id="@+id/ head_left_text"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_alignParentLeft="true"  
  21.         android:text=""   
  22.         android:textColor="#FFFFFF"/>  
  23.           
  24.           
  25.    <Button  
  26.         android:id="@+id/ head_TitleBackBtn"  
  27.         android:layout_width="60dp"     
  28.         android:layout_height="60dp"  
  29.         android:background="@drawable/back"  
  30.         android:gravity="center"  
  31.         android:layout_marginTop="0dp"  
  32.         android:layout_alignParentRight="true"/>  
  33.   
  34. </RelativeLayout>  


 

3 AndroidManifest.xml中应用自定义标题栏

[html]  view plain copy
  1. <application  
  2.         android:name=".IndexActivity"  
  3.         android:icon="@drawable/ic_launchermain"  
  4.         android:label="@string/app_name"  
  5.         android:theme="@style/MyCustomTheme" >  


 

自定义代码封装

[java]  view plain copy
  1. import android.app.Activity;  
  2. import android.view.KeyEvent;  
  3. import android.view.View;  
  4. import android.view.View.OnClickListener;  
  5. import android.view.Window;  
  6. import android.widget.Button;  
  7. import android.widget.TextView;  
  8.   
  9. import com.aaron.util.R;  
  10.   
  11. /** 
  12.  * @author aaron 
  13.  *  
  14.  */  
  15. public class CustomTitleBar {  
  16.   
  17.     private static Activity mActivity;  
  18.   
  19.     /** 
  20.      * @see [自定义标题栏] 
  21.      * @param activity 
  22.      * @param title 
  23.      */  
  24.     public static void getTitleBar(Activity activity,String title) {  
  25.         mActivity = activity;  
  26.         activity.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);  
  27.         activity.setContentView(R.layout.custom_title);  
  28.         activity.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,  
  29.                 R.layout.custom_title);  
  30.         TextView textView = (TextView) activity.findViewById(R.id.head_center_text);  
  31.         textView.setText(title);  
  32.         Button titleBackBtn = (Button) activity.findViewById(R.id.TitleBackBtn);  
  33.         titleBackBtn.setOnClickListener(new OnClickListener() {  
  34.             public void onClick(View v) {  
  35.                 KeyEvent newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,  
  36.                         KeyEvent.KEYCODE_BACK);  
  37.                 mActivity.onKeyDown(KeyEvent.KEYCODE_BACK, newEvent);  
  38.             }  
  39.         });  
  40.     }  
  41. }  


 

5 Activity中添加设置自定义标题代码

[java]  view plain copy
  1. @Override  
  2.     protected void onCreate(Bundle savedInstanceState) {  
  3.         // TODO Auto-generated method stub  
  4.         super.onCreate(savedInstanceState);  
  5.         // ---------------------------------------------------  
  6.         // 自定义Activity标题栏  
  7.         TitleSet. getTitleBar(this,"我的自定义标题栏");  

  1. 6 截图

            // ---------------------------------------------------  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值