您的位置:首页 > 移动开发 > Android开发

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

2016-12-13 17:28 141 查看
今天在项目中需要用到自定义工具栏,制作完成,特记录如下:

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基本就可以实现了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 自定义title