您的位置:首页 > 移动开发 > Android开发

自定义Android手机的Activity标题栏

2014-03-27 23:14 411 查看
先来文字详述的:

当我们想让自己的手机全屏,立马会想到requestWindowFeature(featrueId)函数吧。

一、枚举常量

1.DEFAULT_FEATURES:系统默认状态,一般不需要指定

2.FEATURE_CONTEXT_MENU:启用ContextMenu,默认该项已启用,一般无需指定

3.FEATURE_CUSTOM_TITLE:自定义标题。当需要自定义标题时必须指定。如:标题是一个按钮时

4.FEATURE_INDETERMINATE_PROGRESS:不确定的进度

5.FEATURE_LEFT_ICON:标题栏左侧的图标

6.FEATURE_NO_TITLE:吴标题

7.FEATURE_OPTIONS_PANEL:启用“选项面板”功能,默认已启用。

8.FEATURE_PROGRESS:进度指示器功能

9.FEATURE_RIGHT_ICON:标题栏右侧的图标

以上枚举常量作为你的参考。

接下来看我自定义个标题出来瞧瞧

先上结果图吧



代码如下:
import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.Window;

import android.widget.Toast;

public class MainActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

/*

* 1.DEFAULT_FEATURES:系统默认状态,一般不需要指定

* 2.FEATURE_CONTEXT_MENU:启用ContextMenu,默认该项已启用,一般无需指定

* 3.FEATURE_CUSTOM_TITLE:自定义标题。当需要自定义标题时必须指定。如:标题是一个按钮时

* 4.FEATURE_INDETERMINATE_PROGRESS:不确定的进度

* 5.FEATURE_LEFT_ICON:标题栏左侧的图标

* 6.FEATURE_NO_TITLE:吴标题

* 7.FEATURE_OPTIONS_PANEL:启用“选项面板”功能,默认已启用。

* 8.FEATURE_PROGRESS:进度指示器功能

* 9.FEATURE_RIGHT_ICON:标题栏右侧的图标

*/

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

setContentView(R.layout.main);

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

}

public void add(View v){

Toast.makeText(this, "看懂了没?", 1).show();

}

}

以上加粗标红的两行很重要,而且必须要严格按照上面那样的顺序出现在代码中。即:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);必须出现在super.onCreate(savedInstanceState);之后,setContentView(R.layout.main);之前。其意思就是告诉系统,本程序要自己定义Titlebar;

getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title); 则必须出现在setContentView之后,其意思就是告诉系统,自定义的布局是R.layout.title(即,我们前面编写的titlebar.xml)
附加:
main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello" />

</LinearLayout>

title.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal"

>

<TextView

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:textColor="#FF0000"

android:text="这是我的自定义标题"

/>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="添加"

android:onClick="add"

/>

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: