Android中添加自定义工具栏的方法

今天在项目中需要用到自定义工具栏,制作完成,特记录如下:

1.在Layout文件夹中创建自定义title的xml文件title_customer.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    <ImageButton
        android:id="@+id/launch_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/launcher_icon"
        android:background="@android:color/transparent"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="7dp"
        android:layout_alignParentLeft="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="15dp"
        android:text="手机互联"
        android:layout_toRightOf="@+id/launch_icon"
        android:textColor="@color/title"
        android:textSize="15dp"/>

    <ImageButton
        android:id="@+id/title_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/button_back"
        android:layout_alignParentRight="true"
        android:background="@android:color/transparent"
        android:layout_marginRight="20dp"
        android:layout_marginTop="7dp"/>

</RelativeLayout>
2.编写自定义类

编写自定义的title类XCustomerTitle.java,并定义相关按键的点击事件。

package com.tongseng.customer_title;

import android.app.Activity;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;

/**
 * Created by tongseng on 2016/12/13.
 */

public class CustomerTitle {
	private static Activity mActivity;
	public static void getCustomerTitle(Activity activity,String string){
		mActivity = activity;
		mActivity.getWindow().requestFeature(Window.FEATURE_CUSTOM_TITLE);
		mActivity.setContentView(R.layout.title_customer);
		mActivity.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.title_customer);
		ImageButton back_btn = (ImageButton)activity.findViewById(R.id.title_back);
		back_btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mActivity.finish();
			}
		});
	}
}
3.定义相关的style

<resources>

    <style name="WindowTitleBackground" >
        <item name="android:background">@android:color/transparent</item>
    </style>
    <style name="CustomerTheme" parent="android:Theme">
        <item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowTitleSize">50dp</item>
    </style>

</resources>

4.在AndroidManifest.xml中设置相关的Activity的theme属性

        <activity
            android:name="com.tonseng.customer_title.MainActivity"
            android:theme="@style/CustomerTheme"
            android:screenOrientation="landscape"
            android:configChanges="orientation|keyboardHidden|screenSize">
        </activity>
注意如果不在manifest中设置theme属性的话,在运行的时候会导致APP报android.util.AndroidRuntimeException: You cannot combine custom titles with other title features的错误。

5.在MainActivity中使用自定义的title类的getCustomerTitle(Activity)方法:
需要注意的一点,改该方法必须在相关Activity onCreate中的setContentView之前调用。

package com.tongseng.customer_title;  
  
import android.app.Activity;  
import android.os.Bundle;  
import android.view.Window;  
  
public class MainActivity extends Activity {  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        CustomerTitle.getCustomerTitle(this);
        setContentView(R.layout.main);  
    }     
}

OK,完成上述几个步骤,那么自定义的title基本就可以实现了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值