Android学习笔记_34_自定义窗口标题
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_自定义窗口标题的更多相关文章
- Android 学习笔记二 自定义按钮形状 颜色 点击渐变
问题:自定义按钮的颜色 形状弧度 渐变效果 1.新建自定义属性button_login.xml (借鉴某大神) <?xml version="1.0" encoding=& ...
- Android 学习笔记一 自定义按钮背景图
入门学到的一些组件都是比较规矩的,但在实际应用中,我们需要更多特色的组件,例如一个简单的Button,所以我们必须要自定义它的属性. 遇到的问题:用两张图片来代替按钮,分别表示点击前后 解决方法:用I ...
- Android学习笔记_54_自定义 Widget (Toast)
1.Toast控件: 通过查看源代码,发现Toast里面实现的原理是通过服务Context.LAYOUT_INFLATER_SERVICE获取一个LayoutInflater布局管理器,从而获取一个V ...
- Android学习笔记_41_TabHost自定义标签和TraceView性能测试
一.tabhost第一种用法,通过在帧布局放入定义好的page页面来实现,这样导致在当前activity下代码量比较大. 1.页面布局: | | | | ...
- 【转】 Pro Android学习笔记(二二):用户界面和控制(10):自定义Adapter
目录(?)[-] 设计Adapter的布局 代码部分 Activity的代码 MyAdapter的代码数据源和构造函数 MyAdapter的代码实现自定义的adapter MyAdapter的代码继续 ...
- 【转】 Pro Android学习笔记(七七):服务(2):Local Service
目录(?)[-] Local service代码 调用Local ServiceLocal Service client代码 AndroidManifestxml定义Serviceacitivty的l ...
- 【转】 Pro Android学习笔记(五五):调试和分析(3):adb命令、模拟器控制台和StrictMode
目录(?)[-] adb命令 模拟器Console StrictMode adb命令 我们在学习SQLite的使用,介绍过部分adb命令的使用,见Pro Android学习笔记(五):了解Conten ...
- 【转】 Pro Android学习笔记(四二):Fragment(7):切换效果
目录(?)[-] 利用setTransition 利用setCustomAnimations 通过ObjectAnimator自定义动态效果 程序代码的编写 利用fragment transactio ...
- 【转】 Pro Android学习笔记(十九):用户界面和控制(7):ListView
目录(?)[-] 点击List的item触发 添加其他控件以及获取item数据 ListView控件以垂直布局方式显示子view.系统的android.app.ListActivity已经实现了一个只 ...
随机推荐
- 案例45-crm练习改写客户列表使用struts2&OGNL
1 修改CustomerAction代码 2 修改jsp/customer/list.jsp代码 <%@ page language="java" contentType=& ...
- org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.
今天报了这个异常,这是页面报的 org.springframework.dao.DataIntegrityViolationException: could not execute statement ...
- 【ExtJS】自定义组件datetimefield(二)
接上[ExtJS]自定义组件datetimefield(一) 第三步:添加按钮事件绑定,获取选定的时间 privates:{ finishRenderChildren: function () { v ...
- 负载均衡服务器中存在大量的TIME_WAIT怎么解决
首先需要明白什么是TIME_WAIT.TIME_WAIT是在tcp断开连接时进行四次回收的时候,主动断开端在收到被动关闭端的FIN包并发送ACK包给被动关闭后进入的状态.这个状态默认情况下是2倍的MS ...
- pat03-树2. List Leaves (25)
03-树2. List Leaves (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given a t ...
- javascript 实现函数/方法重载效果
什么是重载? 在C#和JAVA等编程语言中函数重载是指在一个类中可以定义多个方法名相同但是方法参数和顺序不同的方法,以此来实现不同的功能和操作,这就是重载. JS没有重载,只能模拟重载 一般来说,如果 ...
- 从零开始编译属于你的FFmpeg
一.前提: 编译FFmpeg可以是初学者,尤其是对C语言项目,Linux编译不熟悉的的初学者的一道门槛. 我曾经找过很多博客,文章,有些能编译成功,有些则不能.编译通过,能够运行也是云里雾里的.其实最 ...
- Java开发坏境配置
在"系统变量"中设置3项属性,JAVA_HOME,PATH,CLASSPATH(大小写无所谓),若已存在则点击"编辑",不存在则点击"新建" ...
- 自封装ajax
项目中有时候用不到jq,需要了解xmlhttp原理,自己写一套函数请求和发送数据! /* 封装ajax函数 * @param {string}opt.type http连接的方式,包括POST和GET ...
- ezdpl:完全依赖脚本和ssh的自动化部署方案
ezdpl是easy deployment的简写,使用简单的ssh和shell脚本来部署.升级.回滚和重新配置linux服务器. 重要提示:警告:这个项目还处于测试过程中,请仔细阅读说明,并且自己承担 ...