自定义标题栏

自定义标题栏的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 {@Overrideprotected 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();}@Overridepublic 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>

更多相关文章

  1. 高斯模糊效果移植到android系统
  2. 解决“Connection to https://dl-ssl.google.com refused”问题
  3. Android问题总结
  4. 编译友善之背的mini210的android文件系统
  5. android给listview设置分割线Divider样式
  6. 【Android-File】Android文件的读写
  7. 14 Android(安卓)android 按钮效果的两种实现方法
  8. android的文件操作
  9. Android如何在xml布局中使用自定义属性

随机推荐

  1. android网站汇总
  2. Android(安卓)实现点击震动效果
  3. Android(安卓)PopupWindow的使用
  4. [Android设计模式]Android退出应用程序终
  5. JKeyboardPanelSwitch
  6. 从Android开发者的角度去理解RxJava(四)
  7. Android安装步骤
  8. Android:视频(VideoView/MediaPlayer)
  9. Android(安卓)native C 程序中输出LOG信
  10. Android中的sqlite事务操作