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. centOS查看apache版本的命令

    在centOS 7下: 命令如下: httpd -v

  2. Maven工程红色感叹号,且工程无红叉错误

    很可能是jar包不对,可以将maven库里的jar包删除,从 http://mvnrepository.com/ 根据jar包版本号下载到本地maven库,并在pom.xml里引入jar依赖 这次ja ...

  3. 18 Command Line Tools to Monitor Linux Performance

    By Ravi Saive Under: Linux Commands, Monitoring Tools On: December 26, 2013 http://www.tecmint.com/c ...

  4. Java Collection.Set

    package 集合; /** * Set不包含重复元素 存储顺序和取出数据不一样 * * HashSet:它不保证set的迭代顺序,特别是它不保证该顺序恒久不变 * 底层是哈希表结构的 * Link ...

  5. [转] 微信小程序 页面跳转 传递参数

    本文转自:http://blog.csdn.net/qq_31383345/article/details/52795212 微信小程序的页面跳转,页面之间传递参数笔记. CSDN微信小程序开发专栏, ...

  6. 适用于所有页面的基础样式base.css

    @charset "UTF-8"; /*css 初始化 */ html, body, ul, li, ol, dl, dd, dt, p, h1, h2, h3, h4, h5, ...

  7. SQL:Example Uses of the SUBSTRING String Function

    ---Example Uses of the SUBSTRING String Function --http://www.sql-server-helper.com/tips/tip-of-the- ...

  8. vue学习笔记之基础篇

    本文主要记录学习vue的一些基础内容及常用知识点的记录. 1.搭建脚手架 vue init webpack vue-demo 初始化一个使用webpack打包的vue项目 npm install 安装 ...

  9. svn图标显示不正常,文件夹显示但文件不显示svn图标

    svn图标显示不正常,文件夹显示但文件不显示svn图标   这个问题的引发是自己造成的,使用myEclipse时progress会卡在 refresh svn status cache (0%)这里, ...

  10. PrintDocument打印、预览、打印机设置和打印属性的方法(较完整) .

    private void Form1_Load(object sender, System.EventArgs e) { //获取或设置一个值,该值指示是否发送到文件或端口 printDocument ...