我的Android进阶之旅------>Android自定义窗口标题实例

简介:    该实例的功能比较简单,但是通过该实例的扩展可以在自定义标题中做出菜单导航等实用的功能,为了实现自定义窗口标题,需要做以下几个步骤: 1、给自定义标题提供一个界面 2、将自定义标题应用给Activity窗口 3、把android系统为Activit...

  

该实例的功能比较简单,但是通过该实例的扩展可以在自定义标题中做出菜单导航等实用的功能,为了实现自定义窗口标题,需要做以下几个步骤:

1、给自定义标题提供一个界面

2、将自定义标题应用给Activity窗口

3、把android系统为Activity设置的默认主题改为自己的主题


============================下面查看实现该例子的具体代码================================

step1、新建一个项目MyCustomTitle

step2、编写自定义标题的布局文件 /res/layout/custom_title.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="horizontal" android:layout_width="fill_parent"
	android:layout_height="fill_parent" android:background="@drawable/rectangle"> <!-- 指定背景,该背景自己画的 -->

	<Button android:id="@+id/infoAtMeTextView"
		android:textColor="#FF0000" android:layout_width="wrap_content"
		android:layout_height="match_parent" android:layout_weight="1"
		android:gravity="center" android:text="\@我" />
	<Button android:id="@+id/infoCommentTextView"
		android:textColor="#FF0000" android:layout_width="wrap_content"
		android:layout_height="match_parent" android:layout_weight="1"
		android:gravity="center" android:text="评论" />
	<Button android:id="@+id/infoPrivateMsgTextView"
		android:textColor="#FF0000" android:layout_width="wrap_content"
		android:layout_height="match_parent" android:layout_weight="1"
		android:gravity="center" android:text="私信" />
</LinearLayout>

step3、上面布局文件中使用的背景是一个drawable文件,该drawable文件绘制了一个长方形。该文件是/drawable/rectangle.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- 下面定义了一个长方形 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
	android:shape="rectangle">
	<gradient android:angle="270" android:endColor="#1DC9CD"
		android:startColor="#A2E0FB" />
	<padding android:left="2dp" android:top="2dp" android:right="2dp"
		android:bottom="2dp" />
</shape>

step4、将自定义标题设置到Activity中,CustomTitleActivity.java

package cn.oyp.title;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class CustomTitleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //指定使用自定义标题
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.main);
        //设置窗口的自定义标题布局文件
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
        
    }
}

===================================== 读源码开始 ==============================================


然后运行该应用,发现用户设置后的自定义layout没有办法填充整个标题栏。

通过查看Android源代码得知Android系统为Activity的title默认设置了一个布局文件,该布局文件是core/res/res/layout/screen_title.xml


<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
  
          http://www.apache.org/licenses/LICENSE-2.0
  
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<!--
This is an optimized layout for a screen, with the minimum set of features
enabled.
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:fitsSystemWindows="true">
    <FrameLayout
        android:layout_width="match_parent" 
        android:layout_height="?android:attr/windowTitleSize"
        style="?android:attr/windowTitleBackgroundStyle">
        <TextView android:id="@android:id/title" 
            style="?android:attr/windowTitleStyle"
            android:background="@null"
            android:fadingEdge="horizontal"
            android:gravity="center_vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>
    <FrameLayout android:id="@android:id/content"
        android:layout_width="match_parent" 
        android:layout_height="0dip"
        android:layout_weight="1"
        android:foregroundGravity="fill_horizontal|top"
        android:foreground="?android:attr/windowContentOverlay" />
</LinearLayout>

读上一段代码可以发现该布局文件由两个帧布局构成,而其中的几个属性

?android:attr/windowTitleSize 标题高度

?android:attr/windowTitleBackgroundStyle     标题背景样式

?android:attr/windowContentOverlay 标题前景色

而这几个属性的值都是在core/res/res/values/themes.xml文件中被赋值了

	<style name="Theme">
		<item name="android:windowContentOverlay">@android:drawable/title_bar_shadow</item>
		<item name="android:windowTitleSize">25dp</item>
		<item name="android:windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item>
	</style>

而上面的@android:style/WindowTitleBackground样式在core/res/res/values/styles.xml文件中被定义

        <style name="WindowTitleBackground">
		<item name="android:background">@android:drawable/title_bar</item>
	</style>

===================================== 读源码结束 ==============================================

step5、自定义样式 /res/values/style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<!-- 该样式继承系统的默认样式 -->
	<style name="customTheme" parent="android:Theme">
		<!-- 设置标题前景色为透明 -->
		<item name="android:windowContentOverlay">@drawable/nocolor</item>
		<!-- 设置标题高度为44dp -->
		<item name="android:windowTitleSize">44dp</item>
		<!-- 设置标题背景色 -->
		<item name="android:windowTitleBackgroundStyle">@style/customBg</item>
	</style>
	
	<!-- 定义一个背景样式 -->
	<style name="customBg">
		<item name="android:background">@drawable/rectangle</item>
	</style>
</resources>

上面的@drawable/nocolor定义在/res/values/strings.xml文件中

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">该应用的目的是自定义窗口标题!</string>
    <string name="app_name">自定义窗口标题</string>
    <!-- 定义一个透明色 -->
	<drawable name="nocolor">#00000000</drawable>
</resources>

step6、将在自定义的标题样式应用到窗口中,在描述文件AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="cn.oyp.title"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".CustomTitleActivity"
                android:theme="@style/customTheme"><!-- 使用自定义主题 -->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

step7:查看该自定义窗口的效果

=================================================================================================

  作者:欧阳鹏  欢迎转载,与人分享是进步的源泉!

  转载请保留原文地址http://blog.csdn.net/ouyang_peng

==================================================================================================



相关文章
|
4月前
|
Android开发 UED 计算机视觉
Android自定义view之线条等待动画(灵感来源:金铲铲之战)
本文介绍了一款受游戏“金铲铲之战”启发的Android自定义View——线条等待动画的实现过程。通过将布局分为10份,利用`onSizeChanged`测量最小长度,并借助画笔绘制动态线条,实现渐变伸缩效果。动画逻辑通过四个变量控制线条的增长与回退,最终形成流畅的等待动画。代码中详细展示了画笔初始化、线条绘制及动画更新的核心步骤,并提供完整源码供参考。此动画适用于加载场景,提升用户体验。
432 5
Android自定义view之线条等待动画(灵感来源:金铲铲之战)
|
4月前
|
Android开发
Android自定义view之利用PathEffect实现动态效果
本文介绍如何在Android自定义View中利用`PathEffect`实现动态效果。通过改变偏移量,结合`PathEffect`的子类(如`CornerPathEffect`、`DashPathEffect`、`PathDashPathEffect`等)实现路径绘制的动态变化。文章详细解析了各子类的功能与参数,并通过案例代码展示了如何使用`ComposePathEffect`组合效果,以及通过修改偏移量实现动画。最终效果为一个菱形图案沿路径运动,源码附于文末供参考。
|
4月前
|
Android开发 开发者
Android自定义view之利用drawArc方法实现动态效果
本文介绍了如何通过Android自定义View实现动态效果,重点使用`drawArc`方法完成圆弧动画。首先通过`onSizeChanged`进行测量,初始化画笔属性,设置圆弧相关参数。核心思路是不断改变圆弧扫过角度`sweepAngle`,并调用`invalidate()`刷新View以实现动态旋转效果。最后附上完整代码与效果图,帮助开发者快速理解并实践这一动画实现方式。
133 0
|
4月前
|
Android开发 数据安全/隐私保护 开发者
Android自定义view之模仿登录界面文本输入框(华为云APP)
本文介绍了一款自定义输入框的实现,包含静态效果、hint值浮动动画及功能扩展。通过组合多个控件完成界面布局,使用TranslateAnimation与AlphaAnimation实现hint文字上下浮动效果,支持密码加密解密显示、去除键盘回车空格输入、光标定位等功能。代码基于Android平台,提供完整源码与attrs配置,方便复用与定制。希望对开发者有所帮助。
|
4月前
|
XML Java Android开发
Android自定义view之网易云推荐歌单界面
本文详细介绍了如何通过自定义View实现网易云音乐推荐歌单界面的效果。首先,作者自定义了一个圆角图片控件`MellowImageView`,用于绘制圆角矩形图片。接着,通过将布局放入`HorizontalScrollView`中,实现了左右滑动功能,并使用`ViewFlipper`添加图片切换动画效果。文章提供了完整的代码示例,包括XML布局、动画文件和Java代码,最终展示了实现效果。此教程适合想了解自定义View和动画效果的开发者。
217 65
Android自定义view之网易云推荐歌单界面
|
4月前
|
XML 前端开发 Android开发
一篇文章带你走近Android自定义view
这是一篇关于Android自定义View的全面教程,涵盖从基础到进阶的知识点。文章首先讲解了自定义View的必要性及简单实现(如通过三个构造函数解决焦点问题),接着深入探讨Canvas绘图、自定义属性设置、动画实现等内容。还提供了具体案例,如跑马灯、折线图、太极图等。此外,文章详细解析了View绘制流程(measure、layout、draw)和事件分发机制。最后延伸至SurfaceView、GLSurfaceView、SVG动画等高级主题,并附带GitHub案例供实践。适合希望深入理解Android自定义View的开发者学习参考。
538 84
|
4月前
|
Android开发 开发者
Android自定义View之不得不知道的文件attrs.xml(自定义属性)
本文详细介绍了如何通过自定义 `attrs.xml` 文件实现 Android 自定义 View 的属性配置。以一个包含 TextView 和 ImageView 的 DemoView 为例,讲解了如何使用自定义属性动态改变文字内容和控制图片显示隐藏。同时,通过设置布尔值和点击事件,实现了图片状态的切换功能。代码中展示了如何在构造函数中解析自定义属性,并通过方法 `setSetting0n` 和 `setbackeguang` 实现功能逻辑的优化与封装。此示例帮助开发者更好地理解自定义 View 的开发流程与 attrs.xml 的实际应用。
Android自定义View之不得不知道的文件attrs.xml(自定义属性)
|
4月前
|
前端开发 Android开发 UED
讲讲Android为自定义view提供的SurfaceView
本文详细介绍了Android中自定义View时使用SurfaceView的必要性和实现方式。首先分析了在复杂绘制逻辑和高频界面更新场景下,传统View可能引发卡顿的问题,进而引出SurfaceView作为解决方案。文章通过Android官方Demo展示了SurfaceView的基本用法,包括实现`SurfaceHolder.Callback2`接口、与Activity生命周期绑定、子线程中使用`lockCanvas()`和`unlockCanvasAndPost()`方法完成绘图操作。
108 3
|
4月前
|
Android开发 开发者
Android自定义view之围棋动画(化繁为简)
本文介绍了Android自定义View的动画实现,通过两个案例拓展动态效果。第一个案例基于`drawArc`方法实现单次动画,借助布尔值控制动画流程。第二个案例以围棋动画为例,从简单的小球直线运动到双向变速运动,最终实现循环动画效果。代码结构清晰,逻辑简明,展示了如何化繁为简实现复杂动画,帮助读者拓展动态效果设计思路。文末提供完整源码,适合初学者和进阶开发者学习参考。
Android自定义view之围棋动画(化繁为简)
|
10月前
|
缓存 前端开发 Android开发
安卓开发中的自定义视图:从零到英雄
【10月更文挑战第42天】 在安卓的世界里,自定义视图是一块画布,让开发者能够绘制出独一无二的界面体验。本文将带你走进自定义视图的大门,通过深入浅出的方式,让你从零基础到能够独立设计并实现复杂的自定义组件。我们将探索自定义视图的核心概念、实现步骤,以及如何优化你的视图以提高性能和兼容性。准备好了吗?让我们开始这段创造性的旅程吧!
150 1

热门文章

最新文章