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

1 定义标题栏样式

 

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

    <style name="TitleBarBackground">
        <item name="android:background">@color/style_blue</item>
    </style>
          
     <style name="MyCustomTheme" parent="android:Theme">     
         <item name="android:windowTitleBackgroundStyle">@style/ TitleBarBackground</item>
         <item name="android:windowTitleSize">50dp</item>
     </style> 

</resources>


 

2 定义标题栏布局 custom_title.xml

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/screen"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

        <TextView
        android:id="@+id/head_center_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="" 
        android:textColor="#FFFFFF"
        />
    
    <TextView
        android:id="@+id/ head_left_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="" 
        android:textColor="#FFFFFF"/>
    	
    	
   <Button
    	android:id="@+id/ head_TitleBackBtn"
        android:layout_width="60dp"   
        android:layout_height="60dp"
        android:background="@drawable/back"
        android:gravity="center"
        android:layout_marginTop="0dp"
        android:layout_alignParentRight="true"/>

</RelativeLayout>


 

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

<application
        android:name=".IndexActivity"
        android:icon="@drawable/ic_launchermain"
        android:label="@string/app_name"
        android:theme="@style/MyCustomTheme" >


 

4 自定义代码封装

import android.app.Activity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.aaron.util.R;

/**
 * @author aaron
 * 
 */
public class CustomTitleBar {

	private static Activity mActivity;

	/**
	 * @see [自定义标题栏]
	 * @param activity
	 * @param title
	 */
	public static void getTitleBar(Activity activity,String title) {
		mActivity = activity;
		activity.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		activity.setContentView(R.layout.custom_title);
		activity.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.custom_title);
		TextView textView = (TextView) activity.findViewById(R.id.head_center_text);
		textView.setText(title);
		Button titleBackBtn = (Button) activity.findViewById(R.id.TitleBackBtn);
		titleBackBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				KeyEvent newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,
						KeyEvent.KEYCODE_BACK);
				mActivity.onKeyDown(KeyEvent.KEYCODE_BACK, newEvent);
			}
		});
	}
}


 

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

@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// ---------------------------------------------------
		// 自定义Activity标题栏
		TitleSet. getTitleBar(this,"我的自定义标题栏");
		// ---------------------------------------------------
		setContentView(R.layout.main);
	}


 

6 截图

android带返回按钮的自定义标题栏_第1张图片

你可能感兴趣的:(android,layout,application,import,button,encoding)