android--自定义title

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; }
给个效果:




转载于:https://www.cnblogs.com/android-html5/archive/2012/03/12/2534084.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值