1、建好项目之后在它的layout文件夹下创建一个title.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" > <TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/hello_world"
android:textColor="#FF00FF"
/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:text="添加" /> </LinearLayout>

  2、在res/drawable文件下建立rectangle.xml文件,为窗口应用上渐变效果。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 填充色为渐变色,不需要中间颜色startColor开始和结束的颜色.-->
<gradient
android:angle="270"
android:endColor="#1DC9CD"
android:startColor="#A2E0FB"/>
<!-- 定义内间距 -->
<padding
android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp" /> </shape>

  3、布局文件:

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button" /> </RelativeLayout>

  4、通过activity后台代码进行自定义窗口设置。

package com.example.customertitle;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Toast; //自定义标题
public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 1.设置使用自定义窗口
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
// 2.给窗口引入自定义标题的xml界面文件
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
} public void add(View v) {
Toast.makeText(this, "按钮被点击", Toast.LENGTH_LONG).show();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }

  5、部署项目,可以显示自定义的窗口标题。可是自定义的窗口标题距离界面左右两端有一点距离,并没有完全覆盖。为了解决这一个问题,需要覆盖android的窗口标题。下面是android窗口标题的源码。

<!--2. 注意: 系统窗口的界面文件在Android系统源代码android-sdk-windows\platforms\android-8\data\res\layout下的screen_custom_title.xml,内容如下:
1.一个线性布局-->
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:fitsSystemWindows="true">
<!--2.两个帧布局. -->
<FrameLayout android:id="@android:id/title_container"
<!--宽度使用父元素的.-->
android:layout_width="match_parent"
<!--高度使用"?android:attr/windowTitleSize"系统的一个属性的值.-->
android:layout_height="?android:attr/windowTitleSize"
<!--这里还应用了一个样式,这个样式也是使用的系统的一个值.
这里用了一个样式,这个是在windowTitleBackgroundStyle
系统的默认主题里指定的.-->
style="?android:attr/windowTitleBackgroundStyle"> </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" />
<!--3.这两个帧布局的关系,第二个会叠加在第一个帧布局的上面.-->
</LinearLayout>
<!--这里要解决,图片的两端有空白的地方的做法是:让第二个帧布局变成透明的,第二个
利用上次做的背景图.-->
?android:attr/windowTitleSize ?android:attr/windowTitleBackgroundStyle ?android:attr/windowContentOverlay 上述属性的值在android-sdk-windows\platforms\android-8\data\res\values下的themes.xml文件中定义: <style name="Theme"> <itemname="windowContentOverlay">@android:drawable/title_bar_shadow</item> <itemname="windowTitleSize">25dip</item> <itemname="windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item> </style> @android:style/WindowTitleBackground样式在android-sdk-windows\platforms\android-8\data\res\values下的styles.xml文件中定义: <style name="WindowTitleBackground"> <itemname="android:background">@android:drawable/title_bar</item> </style>

  通过上述可以知道android的主题样式,现在需要继承重写它的样式,代码如下

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 定义一个样式,覆盖原有主题样式 -->
<style name="myTheme" parent="android:Theme">
<item name="android:windowContentOverlay">@drawable/color</item>
<item name="android:windowTitleSize">50dp</item>
<item name="android:windowTitleBackgroundStyle">@style/textViewBg</item>
</style> <style name="textViewBg">
<item name="android:background">@drawable/rectangle</item>
</style>
</resources>

  颜色值的定义

<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">CustomerTitle</string>
<string name="action_settings">Settings</string>
<string name="hello_world">自定义标题</string>
<drawable name="color">#00000000</drawable>
</resources>

Android学习笔记_34_自定义窗口标题的更多相关文章

  1. Android 学习笔记二 自定义按钮形状 颜色 点击渐变

    问题:自定义按钮的颜色 形状弧度  渐变效果 1.新建自定义属性button_login.xml (借鉴某大神) <?xml version="1.0" encoding=& ...

  2. Android 学习笔记一 自定义按钮背景图

    入门学到的一些组件都是比较规矩的,但在实际应用中,我们需要更多特色的组件,例如一个简单的Button,所以我们必须要自定义它的属性. 遇到的问题:用两张图片来代替按钮,分别表示点击前后 解决方法:用I ...

  3. Android学习笔记_54_自定义 Widget (Toast)

    1.Toast控件: 通过查看源代码,发现Toast里面实现的原理是通过服务Context.LAYOUT_INFLATER_SERVICE获取一个LayoutInflater布局管理器,从而获取一个V ...

  4. Android学习笔记_41_TabHost自定义标签和TraceView性能测试

    一.tabhost第一种用法,通过在帧布局放入定义好的page页面来实现,这样导致在当前activity下代码量比较大. 1.页面布局: |        |        |        |    ...

  5. 【转】 Pro Android学习笔记(二二):用户界面和控制(10):自定义Adapter

    目录(?)[-] 设计Adapter的布局 代码部分 Activity的代码 MyAdapter的代码数据源和构造函数 MyAdapter的代码实现自定义的adapter MyAdapter的代码继续 ...

  6. 【转】 Pro Android学习笔记(七七):服务(2):Local Service

    目录(?)[-] Local service代码 调用Local ServiceLocal Service client代码 AndroidManifestxml定义Serviceacitivty的l ...

  7. 【转】 Pro Android学习笔记(五五):调试和分析(3):adb命令、模拟器控制台和StrictMode

    目录(?)[-] adb命令 模拟器Console StrictMode adb命令 我们在学习SQLite的使用,介绍过部分adb命令的使用,见Pro Android学习笔记(五):了解Conten ...

  8. 【转】 Pro Android学习笔记(四二):Fragment(7):切换效果

    目录(?)[-] 利用setTransition 利用setCustomAnimations 通过ObjectAnimator自定义动态效果 程序代码的编写 利用fragment transactio ...

  9. 【转】 Pro Android学习笔记(十九):用户界面和控制(7):ListView

    目录(?)[-] 点击List的item触发 添加其他控件以及获取item数据 ListView控件以垂直布局方式显示子view.系统的android.app.ListActivity已经实现了一个只 ...

随机推荐

  1. 深入redis内部之redis启动过程之一

    redis作为一个服务器,它的启动是从main函数开始的.redis.c 1. 进程重命名 #ifdef INIT_SETPROCTITLE_REPLACEMENT spt_init(argc, ar ...

  2. 从数组去重这个函数来体验es6的高效率

    前几天碰到一个题目,要求是这样的. 题目描述 为 Array 对象添加一个去除重复项的方法 示例1 输入 [false, true, undefined, null, NaN, 0, 1, {}, { ...

  3. [转].NET Core之Entity Framework Core 你如何创建 DbContext

    本文转自:http://www.cnblogs.com/tdws/p/5874212.html 本文版权归博客园和作者吴双共同所有,欢迎转载,转载和爬虫请注明博客园蜗牛原文地址 http://www. ...

  4. c#-cs-bs-正则表达式

    C/S     B/S Cs结构:     C/S(Client/Server)客户机/服务器 BS机构:     B/S(Browser/Server)浏览器/服务器       à(未来发展方向) ...

  5. fegin client使用http url合约时报: [Request processing failed; nested exception is feign.FeignException: status 400 reading

    首先看feign client代码: @FeignClient(name = "SPRING-CLOUD-WEB-PROVIDER-GROUP2", url = "htt ...

  6. PAT 1055 The World's Richest

    #include <cstdio> #include <cstdlib> #include <cstring> #include <vector> #i ...

  7. Bootstrap组件介绍

    一.下拉菜单 用于显示链接列表的可切换.有上下文的菜单.下拉菜单的 JavaScript 插件让它具有了交互性. 1.实例 将下拉菜单触发器和下拉菜单都包裹在 .dropdown 里,或者另一个声明了 ...

  8. javascript 权威指南

    1.对象 1.1.序列话对象 JSON.stringify() 和 JSON.parse() 用来序列化和还原 javascript 对象. var o = {x:1, y:{z:[false,nul ...

  9. canvas的getImageData和putImageDataAPI

  10. PLSQL Developer乱码

    1.select * from v$nls_parameters 查询nls的参数,获得数据库服务器端的字符编码 NLS_LANGUAGE NLS_CHARACTERSET 2.修改本地环境变量,设置 ...