菜鸟初学android体验之——实现自定义简单标题栏的两种简单方法

本文面向安卓初学者,介绍了实现自定义标题栏的两种简单方法。第一种方法涉及创建XML布局文件,定义样式,并在Java代码中设置监听事件。第二种方法则是在布局文件中直接创建伪标题栏,通过Java代码控制,适用于多个Activity的定制。总结中提到第二种方法更简单,能实现不同Activity的个性化标题栏设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


首先是参照了网上一些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" >
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="fill_parent"
android:background="#d0d0d0">
<Button
android:id="@+id/btn_left"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:background="#ffffff"
android:text="..." />
<Button
android: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文件
    
<application
android: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;
@Override
protected 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);
}
 
@Override
public 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 stub
switch (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;
@Override
protected 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);
}
 
@Override
public 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;
}
 
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (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" >
 
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#d0d0d0" >
<Button
android:id="@+id/btn_right"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:background="#ffffff"
android:text="..." />
<Button
android: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文件修改  
application
android: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中设置不同的自定“标题栏”(原因是不是真正的标题栏),另外还有好多高级的实现自定义标题栏,由于鄙人初学者伤害不起啊,有不正之处,望见谅!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值