自定义标题栏
自定义标题栏的xml文件title_bar.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:layout_width="wrap_content" android:layout_centerVertical="true" android:layout_height="wrap_content" android:src="@drawable/tit_back" android:contentDescription="@string/back" android:onClick="show" /> <TextView android:layout_width="wrap_content" android:layout_centerInParent="true" android:layout_height="wrap_content" android:text="@string/app_name" /> </RelativeLayout> 标题栏样式xml style.xml
<style name="CustomWindowTitleBackground"> <item name="android:background">@drawable/bg_titlebar</item> </style> <style name="test" parent="android:Theme" mce_bogus="1"> <item name="android:windowTitleSize">40dip</item> <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item> </style>
主要代码:
package com.su.mytitile.activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); //设置自定义标题栏 注意顺序
setContentView(R.layout.activity_main);//注意顺序
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar); //titlebar为自己标题栏的布局 注意顺序
}
public void show(View view){
Toast.makeText(MainActivity.this, "标题栏", Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
drawable目录下tit_back.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <!-- Non focused states --> <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tit_back_0" /> <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tit_back_1" /> <!-- Focused states --> <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tit_back_1" /> <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tit_back_1" /> <!-- Pressed --> <item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tit_back_1" /> <item android:state_pressed="true" android:drawable="@drawable/tit_back_1" /> </selector>
修改AndroidManifest.xml文件
<application android:allowBackup="true" > <activity android:name="com.su.mytitile.activity.MainActivity" android:theme="@style/test" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>