<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/re_title" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dp" //定义自定义标题栏的高度
android:background="@color/start_background"
android:orientation="horizontal">
<ImageButton
android:scaleType="fitXY"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:id="@+id/bt_back"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/left_back"
android:background="@color/touming"/>
<TextView
android:id="@+id/mytitle"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"//使文字在整个标题栏的中间
android:textColor="#fff"
android:textSize="20dp" />
</RelativeLayout >
<resources>
<!-- 自定义标题栏 parent="android:Theme" 这个属性必须写 -->
<style name="mytitlestyle" parent="android:Theme">
<!-- 设置高度,和 mytitlebar.xml中保持一致 -->
<item name="android:windowTitleSize">50dp</item>
<!-- 设置内填充为0 使自定义标题填充整个标题栏,否则左右两边有空隙 -->
<item name="android:padding">0dp</item>
</style>
</resources>
public class CustomTitleBar {
private Activity mActivity;
//不要使用 static 因为有三级页面返回时会报错
/**
* @param activity
* @param title
* @see [自定义标题栏]
*/
public void getTitleBar(Activity activity, String title) {
mActivity = activity;
activity.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
//指定自定义标题栏的布局文件
activity.setContentView(R.layout.mytitlebar);
activity.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.mytitlebar);
//获取自定义标题栏的TextView控件并设置内容为传递过来的字符串
TextView textView = (TextView) activity.findViewById(R.id.mytitle);
textView.setText(title);
//设置返回按钮的点击事件
ImageButton titleBackBtn = (ImageButton) activity.findViewById(R.id.bt_back);
titleBackBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//调用系统的返回按键的点击事件
mActivity.onBackPressed();
}
});
}
}
public class food extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//实例化CustomTitleBar 传递相应的参数
CustomTitleBar ct = new CustomTitleBar();
ct.getTitleBar(this, "美食");
setContentView(R.layout.page_food);
}
}
//省略了其余部分,android:theme="@style/mytitlestyle"这句必需写
<activity
android:name=".food"
android:label="@string/activity_food"
android:theme="@style/mytitlestyle" />
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有