Android 自定义标题栏

来源:互联网 发布:徐文长手对知俯 编辑:程序博客网 时间:2024/05/17 09:25

转自:http://lovezhou.iteye.com/blog/849953


核心代码:
//设置标志为自定义标题栏
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.custom_title);
//设置自定义标题栏,该句必须放在setContentView之后
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_1);
效果图:


Java代码  收藏代码
  1. package com.zhou.activity;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.Window;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10. import android.widget.TextView;  
  11.   
  12. public class CustomTitleActivity extends Activity {  
  13.     /** Called when the activity is first created. */  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         //关键代码  
  18.         //设置自定义标题栏标志  
  19.         requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);  
  20.         //显示页面  
  21.         setContentView(R.layout.custom_title);  
  22.         //设置自定义标题栏,改句必须放在setContentView之后  
  23.         getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_1);  
  24.           
  25.         final TextView leftText = (TextView) findViewById(R.id.left_text);  
  26.         final TextView rightText = (TextView) findViewById(R.id.right_text);  
  27.         final EditText leftTextEdit = (EditText) findViewById(R.id.left_text_edit);  
  28.         final EditText rightTextEdit = (EditText) findViewById(R.id.right_text_edit);  
  29.         Button leftButton = (Button) findViewById(R.id.left_text_button);  
  30.         Button rightButton = (Button) findViewById(R.id.right_text_button);  
  31.         //按钮事件处理  
  32.         leftButton.setOnClickListener(new OnClickListener() {  
  33.             public void onClick(View v) {  
  34.                 leftText.setText(leftTextEdit.getText());  
  35.             }  
  36.         });  
  37.         rightButton.setOnClickListener(new OnClickListener() {  
  38.             public void onClick(View v) {  
  39.                 rightText.setText(rightTextEdit.getText());  
  40.             }  
  41.         });  
  42.     }  

原创粉丝点击