2013/5/26
Java技术qq交流群:JavaDream:251572072
62_自定义窗口标题
---------------------------
下面是一个案例,用来介绍62_自定义窗口标题的实现:
-------------------------------------------------
1.新建android项目:customtitle]
2./customtitle/src/com/credream/customtitle/CustomtitleActivity.java
package com.credream.customtitle;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Toast;
public class CustomtitleActivity extends Activity {
/** Called when the activity is first created. */
/*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>
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 1.设置使用自定义窗口
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
// 2.给窗口引入自定义标题的xml界面文件
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
}
// 当点击了自定义标题中的按钮会执行这个方法,可以做成菜单导航.
public void add(View v){
Toast.makeText(this, "按钮被点击", 1).show();
}
}
------------------------------------------------------------------------
2./customtitle/res/drawable/rectangle.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 新建一个矩形的图片,需要在res下,新建一个文件夹drawable,
然后定义设置:rectangle为矩形 ,定义完了之后就生成了一个矩形图片
-->
<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./customtitle/res/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"
>
<!-- android:orientation="horizontal"
水平的.
-->
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#FF0000"
android:text="这是我的自定义标题"
/>
<!-- 在自定义标题中显示一个按钮
-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加"
android:onClick="add"
/>
</LinearLayout>
------------------------------------------------------
4./customtitle/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, MainActivity!</string>
<string name="app_name">自定义窗口标题</string>
<!-- 这个定义在了这里,用于在styles.xml中进行引用,因为styles.xml只能
引用id. -->
<drawable name="nonecolor">#00000000</drawable>
</resources>
-------------------------------------------------
5./customtitle/res/values/styles.xml
-------------------------------------------------------
2013/5/26
开始出现布局文件cannot be resolved or is not a field,
找解决方法是删除R包,删掉R包,引用R时出现Rcannot be resolved or is not a field
去除代码activity代码页面顶部中的 import android.R;这句就可以消除红色波浪线的
main cannot be resolved or is not a field类似这个错误了
-----------------------------------------------------------------