现在的位置: 首页 > 综合 > 正文

android–自定义title

2013年10月23日 ⁄ 综合 ⁄ 共 4252字 ⁄ 字号 评论关闭

android默认的title只是显示字符串,有时候为了吸引用户的眼球或者操作方便,得搞点个性化的东西。

实现方法是在onCreate()方法开始,加入

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);  
setContentView(view);  
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);  

注意这三行代码的顺序不能随意改变。


在R.layout.title

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
  	android:layout_height="fill_parent">
  		<ImageView
  		android:id="@+id/Titlhome"   
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"   
        android:layout_alignParentLeft="true"
        android:layout_marginTop="3dp"
        android:src="@drawable/home"/>   
  	<TextView
  		android:id="@+id/Titletext"   
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"   
        android:layout_centerHorizontal="true"
        android:textSize="21dp"
        android:layout_marginTop="3dp"
        android:textColor="#ffffffff"
        android:textStyle="bold"/>   
	<Button
		android:id="@+id/TitleBackBtn"
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"
        android:background="@drawable/returnback"
        android:gravity="center"
        android:layout_marginTop="9dp"
        android:layout_alignParentRight="true"/>
</RelativeLayout>


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
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;

public class TitleBarBackActivity extends Activity {
	private TextView tvTitle;
	private Button btnBack;
	private Button btnNext;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.CustomTheme);
    	super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
        tvTitle =(TextView) findViewById(R.id.Titletext);
        tvTitle.setText("标题栏返回按钮测试界面");
        btnBack =(Button) findViewById(R.id.TitleBackBtn);
        btnBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			KeyEvent newEvent =new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_BACK);
			onKeyDown(KeyEvent.KEYCODE_BACK, newEvent);
			}
		});
        
        btnNext =(Button) findViewById(R.id.Button);
        btnNext.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent  =new Intent();
				intent.setClass(TitleBarBackActivity.this,Next.class);
				startActivity(intent);
			}
		});
        
    }
    

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			// 按下的如果是BACK,同时没有重复
			askForOut();

			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	private void askForOut() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		builder.setTitle("确定退出").setMessage("确定退出?").setPositiveButton("确定",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				}).setNegativeButton("取消",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				}).setCancelable(false).show();
	}

}

为了让title里内容能看清,设置下他的style

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
	<style name="CustomTheme" parent="android:Theme">  
		<item name="android:windowTitleSize">40dip</item>  
	</style>  
</resources> 

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

public class Next extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		setTheme(R.style.CustomTheme);
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.next);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);

		tvTitle = (TextView) findViewById(R.id.Titletext);

		tvTitle.setText("标题栏返回按钮测试界面2");

		btnBack = (Button) findViewById(R.id.TitleBackBtn);

		btnBack.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				System.out.println("Back  ..........");
				KeyEvent newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,
						KeyEvent.KEYCODE_BACK);
				onKeyDown(KeyEvent.KEYCODE_BACK, newEvent);
			}
		});
	}

	public Button btnBack;
	public TextView tvTitle;
}

给个效果:


抱歉!评论已关闭.