前言

在安卓中不喜欢系统默认的标题栏,那么如何让自定义一个自己的标题栏呢。

正文

在Android中去掉activity的标题栏有两种方法。

  • 一个是在Activity代码里实现
@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //去掉标题栏        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.activity_main);    }

记住:该代码要写在setContentView()前面。

  • 一个则是在AndroidManifest.xml文件里实现
"true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        //去掉标题栏                android:theme="@android:style/Theme.NoTitleBar">

当然,这样是将整个应用设置成无标题栏。如果只需要在一个Activity设置成一个无标题栏的形式,只要把代码写到某一个Activity里面就可以了。如:

<activity            android:name=".MainActivity"            android:theme="@android:style/Theme.NoTitleBar">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            intent-filter>        activity>

然后,就是介绍如何自定义一个标题栏。

  • 首先,需要定义一个自定义的标题栏布局 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="50dp"    android:orientation="horizontal"    android:background="#f2f8f8">    //layout_height设置新标题栏的高度    <TextView        android:layout_centerInParent="true"        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:text="自定义标题栏"        android:textSize="20sp"        android:gravity="center_vertical"        />RelativeLayout>
  • 然后,在Activity中声明使用自定义的标题栏
    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //声明使用自定义的标题栏        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);        setContentView(R.layout.activity_main);        //将title.xml布局作为自定义标题栏        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);    }
  • 当然,有时候会出现自定义标题栏两侧没有完全覆盖屏幕导致的不美观

这个时候,我们需要再定义一个style文件 MyStyle.xml

<?xml version="1.0" encoding="utf-8"?><resources xmlns:android="http://schemas.android.com/apk/res/android">    <style name="titleBarStyle" parent="android:Theme">        <item name="android:windowTitleSize">50dpitem>-- 以你的自定义标题栏的高度为准 -->        <item name="android:padding">0dpitem>-- 使新的标题栏完全延伸到对齐到原始标题栏的两边 -->    style>resources>

并且,在 AndroidManifest.xml 中给activity 添加 一个theme属性

<activity            android:name=".MainActivity"            android:theme="@style/titleBarStyle">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            intent-filter>        activity>

到这里基本就实现了自定义的标题栏。


以上就是笔者对于Android标题栏的随笔了。

更多相关文章

  1. Android(安卓)MultiDex 解析与使用
  2. Android实用插件,持续更新
  3. PackageInstaller 原理简述
  4. Android子线程不可以刷新UI么?
  5. Android(安卓)- JNI加入标准C++文件
  6. 仿照TapTap做的一个demo,运用了Rxjava+Retrofit+MaterialDesign
  7. 使用内部(com.android.internal)和隐藏(@hide)API手记
  8. Android如何将程序打成jar包
  9. Android(安卓)Studio下OpenCV及JNI开发

随机推荐

  1. 为Android内核添加新驱动,并添加到menucon
  2. Android之Animation
  3. android ndk 使用第三方静态库
  4. No IDEA annotations attached to the JD
  5. android中Message机制的灵活应用
  6. android开发积累4-android使用HttpURLCon
  7. Android类库介绍
  8. Android(安卓)模拟器(emulator)无法启动A
  9. android点滴(15)--ubuntu下配置Android(
  10. Android布局容器