Android中添加自定义工具栏的方法
2016-12-13 17:28
141 查看
今天在项目中需要用到自定义工具栏,制作完成,特记录如下:
1.在Layout文件夹中创建自定义title的xml文件title_customer.xml:
编写自定义的title类XCustomerTitle.java,并定义相关按键的点击事件。
4.在AndroidManifest.xml中设置相关的Activity的theme属性
5.在MainActivity中使用自定义的title类的getCustomerTitle(Activity)方法:
需要注意的一点,改该方法必须在相关Activity onCreate中的setContentView之前调用。
OK,完成上述几个步骤,那么自定义的title基本就可以实现了。
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 两种为自定义组件添加属性的使用方法和区别 推荐
- 关于Android,附赠的sdk sample在eclipse下的添加方法
- Android: ListView添加animation后,在animation播放过程中背景变为黑色背景的解决方法
- Android 关于XML的解析和添加方法
- 笔记:解决eclipse中android添加重载函数时参数为arg0,arg1的方法---兼谈Window下载android SDK
- Android 小方法(一)--随时添加更新
- 说说Android 两种为自定义组件添加属性的使用方法和区别
- 为Android项目添加Proguard混淆编译的简单方法
- Mac 下面,添加android adb命令(一般环境变量的添加方法)
- android 1.6简便添加监听器的方法(转)
- Android系统添加全局快捷键的方法
- Android、iOS添加子视图方法比较
- Android添加开机音乐的具体方法
- 基于Unity3D的Android游戏添加google广告的方法——使用AdMob
- 说说Android 两种为自定义组件添加属性的使用方法和区别
- Android添加单元测试的方法与步骤
- 如何向android的framework里添加新类,新方法
- Android添加自定义View的方法
- android 给组件添加事件的几种方法
- 4.Android添加背景音乐的方法