自定义标题栏

Android允许对手机屏幕的特征进行客户化,例如,客户化标题栏。如果我们想要对某一特征进行客户化,首先就要通过Window.requestFeature (int featureId) 方法进行申请。一旦申请了就不能去掉这个特征了。

Android事先为可能被客户化的特征定义了常量。

int DEFAULT_FEATURES The default features enabled 
int FEATURE_CONTEXT_MENU Flag for the context menu.
int FEATURE_CUSTOM_TITLE Flag for custom title.
int FEATURE_INDETERMINATE_PROGRESS Flag for indeterminate progress 
int FEATURE_LEFT_ICON Flag for having an icon on the left side of the title bar 
int FEATURE_NO_TITLE Flag for the "no title" feature, turning off the title at the top of the screen.
int FEATURE_OPTIONS_PANEL Flag for the "options panel" feature.
int FEATURE_PROGRESS Flag for the progress indicator feature 
int FEATURE_RIGHT_ICON Flag for having an icon on the right side of the title bar 

下面以API Demo中Custom Title为例加以说明。

1. 首先创建客户化的title。

Xml代码

Xml代码

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/screen"  
  3.     android:layout_width="match_parent" android:layout_height="match_parent"  
  4.     android:orientation="vertical">  
  5.     <TextView android:id="@+id/left_text"  
  6.         android:layout_width="wrap_content"  
  7.         android:layout_height="wrap_content"  
  8.         android:layout_alignParentLeft="true"  
  9.         android:text="@string/custom_title_left" />  
  10.     <TextView android:id="@+id/right_text"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_alignParentRight="true"  
  14.         android:text="@string/custom_title_right" />  
  15. </RelativeLayout>  


2. 在CustomTitle.java中将客户化的title应用到activity中。

Java代码

[java] view plain copy
  1. package com.example.android.apis.app;  
  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. import com.example.android.apis.R;  
  13.   
  14. public class CustomTitle extends Activity  
  15.     @Override  
  16.         protected void onCreate(Bundle savedInstanceState)  
  17.         super.onCreate(savedInstanceState);  
  18.   
  19.         requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);  
  20.         setContentView(R.layout.custom_title);  
  21.         getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_1);  
  22.           
  23.         final TextView leftText (TextView) findViewById(R.id.left_text);  
  24.         final TextView rightText (TextView) findViewById(R.id.right_text);  
  25.         final EditText leftTextEdit (EditText) findViewById(R.id.left_text_edit);  
  26.         final EditText rightTextEdit (EditText) findViewById(R.id.right_text_edit);  
  27.         Button leftButton (Button) findViewById(R.id.left_text_button);  
  28.         Button rightButton (Button) findViewById(R.id.right_text_button);  
  29.           
  30.         leftButton.setOnClickListener(new OnClickListener()  
  31.             public void onClick(View v)  
  32.                 leftText.setText(leftTextEdit.getText());  
  33.              
  34.         });  
  35.         rightButton.setOnClickListener(new OnClickListener()  
  36.             public void onClick(View v)  
  37.                 rightText.setText(rightTextEdit.getText());  
  38.              
  39.         });  
  40.      
  41.  


先在setContentView之前调用requestWindowFeature(Window.FEATURE_CUSTOM_TITLE),申请 对标题进行客户化。然后调用getWindow().setFeatureInt将新的title应用到activity上。

客户化window feature时需要注意:

1. 要在setContentView之前调用requestFeature。由于requestWindowFeature = getWindow().requestFeature,所以调用requestWindowFeature 也可。


2. 不能将其他title相关feature与FEATURE_CUSTOM_TITLE一起使用。

 

3. 相关feature的官方文件: http://developer.android.com/reference/android/view/Window.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值