首先是参照了网上一些daemo,结合个人理解菜鸟自己来了一发,初学有很多不足,只起一个引导作用和初入门的小伙伴们一起学习,高手请掠过
1、定义一个title_bar.xml文件,相当于自定义标题栏的布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal" ><RelativeLayoutandroid:layout_height="match_parent"android:layout_width="fill_parent"android:background="#d0d0d0"><Buttonandroid:id="@+id/btn_left"android:layout_width="80dp"android:layout_height="50dp"android:layout_alignParentRight="true"android:background="#ffffff"android:text="..." /><Buttonandroid:id="@+id/btn_left"android:layout_width="80dp"android:layout_height="50dp"android:layout_alignParentLeft="true"android:background="#ffffff"android:text="返回" /></RelativeLayout></LinearLayout>
2、添加styles
2.1 在values文件下的styles.xml文件下加入style
<style name="MyApptheme" parent="android:Theme"><!--可以修改标题栏大小和背景等 --><item name="android:windowTitleSize">50dp</item></style>
2.2 修改AndroidManifels.xml文件
<applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"<!--将文件默认的style改为上面2.1中自己定义的style-- >android:theme="@style/MyApptheme" ><activity
3、java代码
package com.example.titlebartest;import android.os.Bundle;import android.annotation.SuppressLint;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.SearchView.OnCloseListener;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener {private Button btn_left = null;private Button btn_right = null;@Overrideprotected 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);btn_left = (Button) findViewById(R.id.btn_left);btn_left.setOnClickListener(this);btn_right = (Button) findViewById(R.id.btn_right);btn_right.setOnClickListener(this);}@Overridepublic 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;}// 为标题栏里边的控件设置监听事件,做自己想做的事public void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.btn_left:Toast.makeText(this, "左边按钮", Toast.LENGTH_SHORT).show();break;case R.id.btn_right:Toast.makeText(this, "左边按钮", Toast.LENGTH_SHORT).show();break;default:break;}}}
注: 注意下面这三句出场顺序,否则出错,想想也应该是这个顺序吧
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);setContentView(R.layout.activity_main);getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
二、第二种,相当于定义一种伪标题栏,投机倒把
1、先上java文件,对比一下第一种,略有几句改动
package com.example.titlebartest;import android.os.Bundle;import android.annotation.SuppressLint;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.SearchView.OnCloseListener;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener {private Button btn_left = null;private Button btn_right = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 设置为没有标题栏,也可以在AndroidManifest.xml文件设置requestWindowFeature(Window.FEATURE_NO_TITLE);// 请求添加自定义标题栏// requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);setContentView(R.layout.activity_main);// 设置自定义标题栏布局// getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);btn_left = (Button) findViewById(R.id.btn_left);btn_left.setOnClickListener(this);btn_right = (Button) findViewById(R.id.btn_right);btn_right.setOnClickListener(this);}@Overridepublic 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;}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.btn_left:Toast.makeText(this, "左边按钮", Toast.LENGTH_SHORT).show();break;case R.id.btn_right:Toast.makeText(this, "右边按钮", Toast.LENGTH_SHORT).show();break;default:break;}}}
2、这次不需要title_bar.xml布局,直接在activity_main.xml布局,绿色部分即为伪标题栏。这种做法,就是去掉标题栏之后,在整个视图中从头开始布局,第一个布局就相当于标题栏了
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity" ><RelativeLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:background="#d0d0d0" ><Buttonandroid:id="@+id/btn_right"android:layout_width="80dp"android:layout_height="50dp"android:layout_alignParentRight="true"android:background="#ffffff"android:text="..." /><Buttonandroid:id="@+id/btn_left"android:layout_width="80dp"android:layout_height="50dp"android:layout_alignParentLeft="true"android:background="#ffffff"android:text="返回" /></RelativeLayout></RelativeLayout>
3、AndroidManifest.xml文件修改
applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/MyApptheme" ><!--这句设置的样式就不起作用了,当在java文件中不写 "requestWindowFeature(Window.FEATURE_NO_TITLE)" 这句时,这句改为"android:theme="android:theme="@android:style/Theme.Black.NoTitleBar" 也可以实现相同效果"--><activity
总结:第二种个人感觉简单好多,可以实现在很多个activity中设置不同的自定“标题栏”(原因是不是真正的标题栏),另外还有好多高级的实现自定义标题栏,由于鄙人初学者伤害不起啊,有不正之处,望见谅!
本文面向安卓初学者,介绍了实现自定义标题栏的两种简单方法。第一种方法涉及创建XML布局文件,定义样式,并在Java代码中设置监听事件。第二种方法则是在布局文件中直接创建伪标题栏,通过Java代码控制,适用于多个Activity的定制。总结中提到第二种方法更简单,能实现不同Activity的个性化标题栏设计。

被折叠的 条评论
为什么被折叠?



