在应用过程中需要将Activity的标题栏进行修改,一般情况下,通过java代码的setTitle方法可以设置其标题内容。在实际应用过程中可能会有复杂的需求,此时需要进行相关的处理,比如在标题栏上显示两个按钮和一个文本,按钮进行操作可以进行操作。这个时候就需要进行处理,处理方式如下:
1、建立title所需的布局文件:
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_horizontal"
>
<Button android:text="返回"
android:id="@+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btn_middle_normal"
android:layout_marginLeft="20dp"
/>
<TextView android:text="欢迎使用"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:typeface="monospace"
android:layout_marginLeft="15dp"
/>
<Button android:text="下一步"
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btn_middle_normal"
android:textSize="15dp"
android:typeface="monospace"
android:layout_marginLeft="15dp"
/>
</LinearLayout>
2、在java代码部分进行设置
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); // 注意顺序
setContentView(R.layout.declaration); // 注意顺序
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, // 注意顺序
R.layout.title);
}
到此步骤可以修改标题栏,但是不能设置标题栏的大小和背景。还需要处理
3、编写style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="CustomWindowTitleBackground">
<item name="android:background">#565656</item>设置背景
</style>
<style name="test" parent="android:Theme">
<item name="android:windowTitleSize">50dp</item> 设置标题栏大小
<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
</style>
</resources>
4在清单文件中配置
<activity android:name=".DeclarationActivity"
android:label="@string/app_name"
android:theme="@style/test"
>
这样可以完成配置了。如果想完成更复杂的标题栏,那么需要通过java代码进行自定义。