android自定标题栏

自定义标题栏

自定义标题栏的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>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值