1. 标题栏显示图标
但实际效果呢,我觉得不好看,和旁边的文字有相当距离!看看别人的图片的:
效果图:
你还可以添加其他控件,而这些控件的获取和事件响应都是直接在activity里面完成。
标题进度条显示
public
void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.main);
getWindow().setFeatureDrawableResour ce(Window.FEATURE_LEFT_ICON,
android.R.drawable.icon);
// ...
}
}
![自定义Activity标题栏(Title <wbr>bar)和窗体显示状态操作【转】](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
当然这个图标也可以通过自定义布局,使用ImageView来实现:
< ?
xml
version
="1.0"
encoding
="utf-8"
?>
<</font>LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<</font>ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"/>
<</font>TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="文本" />
</</font>LinearLayout>
<</font>LinearLayout
</</font>LinearLayout>
![自定义Activity标题栏(Title <wbr>bar)和窗体显示状态操作【转】](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
2.自定义布局
看看我自定义的标题栏:
![自定义Activity标题栏(Title <wbr>bar)和窗体显示状态操作【转】](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
布局代码(titlebar.xml)
< ?
xml
version
="1.0"
encoding
="utf-8"
?>
<</font>LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<</font>TextView
android:text="@string/app_name"
android:textColor="#000"
android:paddingRight="3.0dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<</font>TextView
android:text="@string/battery_text"
android:textColor="#000"
android:paddingRight="3.0dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<</font>TextView
android:id="@+id/battery_text"
android:textColor="#00f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</</font>LinearLayout>
<</font>LinearLayout
>
</</font>LinearLayout>
Java代码:
public
void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
//自定义标题栏
mWindow = getWindow();
mWindow.setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.titlebar);
mBatteryText = (TextView)findViewById(R.id.battery_text);
mBatteryInforeceiver =
new
BroadcastReceiver(){
@Override
public
void
onReceive(Context context, Intent intent) {
int
level = intent.getIntExtra(
"level", 0);
int
scale = intent.getIntExtra(
"scale", 1);
mBatteryText.setText(String.valueOf((
int)(level*100/scale))+
"%");
}
};
}
}
3. 设置标题栏的背景色和高度
虽然我们可以通过自定义布局文件在标题栏加入一些控件,但是仍然不能改变标题栏的高度、背景色,要想达到这个目的,只能使用theme(主题)。
\res\values\style.xml:
< ?
xml
version
="1.0"
encoding
="utf-8"
?>
<</font>resources>
<</font>style name="CustomWindowTitleBackgro und">
<</font>item name="android:background">#47B2FF</</font>item>
</</font>style>
<</font>style name="activityTitlebar" parent="android:Theme">
<</font>item name="android:windowTitleSize">34dp</</font>item>
<</font>item name="android:windowTitleBackgroundSty le">@style/CustomWindowTitleBackgro und</</font>item>
</</font>style>
</</font>resources>
<</font>resources>
</</font>resources>
窗体显示状态操作(requestWindowFeature()的应用)
首先介绍一个重要方法那就是requestWindowFeature(featrueId),它的功能是启用窗体的扩展特性。参数是Window类中定义的常量。
一、枚举常量
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:标题栏右侧的图标
对于默认启用的和前面有介绍的就略去不提了。我们说比较常用的FEATURE_INDETERMINATE_PROGRESS和FEATURE_NO_TITLE。
FEATURE_INDETERMINATE_PROGRESS:表示一个进程正在运行
progress.xml
< ?
xml
version
="1.0"
encoding
="utf-8"
?>
<</font>LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<</font>ProgressBar android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
style="?android:attr/progressBarStyleSmallTit le">
</</font>ProgressBar>
</</font>LinearLayout>
<</font>LinearLayout
</</font>LinearLayout>
Java代码
public
void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS, R.layout.progress);
setProgressBarIndetermin
ateVisibility(
true);
//适当时候set false来隐藏
//...
}
}
![自定义Activity标题栏(Title <wbr>bar)和窗体显示状态操作【转】](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
FEATURE_NO_TITLE
就是不显示标题栏,某些时候全屏需要,但全屏不等于不显示标题栏,我尝试显示标题栏的同时全屏来去掉系统的状态栏:
Java代码
public
void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
//自定义标题栏
mWindow = getWindow();
mWindow.setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.titlebar);
mWindow.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// ...
}
}
所以真正实现全屏的是后面的那句话!