android自定义窗口标题示例分享

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">
    <FrameLayout android:id="@android:id/title_container"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/windowTitleSize"
        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" />
 </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>

时间: 2024-07-30 12:35:39

android自定义窗口标题示例分享的相关文章

android自定义窗口标题示例分享_Android

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进阶之旅------&amp;gt;Android自定义窗口标题实例

   该实例的功能比较简单,但是通过该实例的扩展可以在自定义标题中做出菜单导航等实用的功能,为了实现自定义窗口标题,需要做以下几个步骤: 1.给自定义标题提供一个界面 2.将自定义标题应用给Activity窗口 3.把android系统为Activity设置的默认主题改为自己的主题 ============================下面查看实现该例子的具体代码================================ step1.新建一个项目MyCustomTitle step2.编

.net自定义事件示例分享

 这篇文章主要介绍了.net自定义事件示例,代码实现了热水器加热,报警器监控,当热水温度达到80度的时候报警器报警这样一个简单的事件处理程序,需要的朋友可以参考下 1.新建一个控制台应用程序TestDelegate,本项目主要实现:热水器加热,报警器监控,当热水温度达到80度的时候报警器报警这样一个简单的事件处理程序   2.定义委托处理程序    代码如下: public delegate void PlayGameHandler(object sender, System.EventArgs

android自定义按钮示例(重写imagebutton控件实现图片按钮)_Android

由于项目这种类型的图片按钮比较多,所以重写了ImageButton类. 复制代码 代码如下: package me.henji.widget; import android.content.Context;import android.graphics.ColorMatrix;import android.graphics.ColorMatrixColorFilter;import android.util.AttributeSet;import android.view.MotionEvent

Android自定义View示例(三)—滑动控件

MainActivity如下: package cc.testview4; import cc.testview4.SlideView.SwitchChangedListener; import android.app.Activity; import android.os.Bundle; /** * Demo描述: * 自定义滑动控件 * * 参考资料: * http://blog.csdn.net/lfdfhl/article/details/8195441 * * 备注说明: * 在Cop

Android自定义View示例(四)—带有动画的Dialog

MainActivity如下: package cc.testview1; import android.os.Bundle; import android.app.Activity; /** * Demo描述: * 自定义Dialog,在Dialog中有动画(旋转动画或者帧动画)效果 */ public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceStat

Android自定义View示例(二)—滑动开关

MainActivity如下: package cc.testview3; import cc.testview3.SwitchView.SwitchChangedListener; import android.os.Bundle; import android.widget.Toast; import android.app.Activity; /** * Demo描述: * 自定义View实现滑动开关 * * 测试设备: * 分辨率为480x854 */ public class Main

android自定义按钮示例(重写imagebutton控件实现图片按钮)

由于项目这种类型的图片按钮比较多,所以重写了ImageButton类. 复制代码 代码如下:package me.henji.widget; import android.content.Context;import android.graphics.ColorMatrix;import android.graphics.ColorMatrixColorFilter;import android.util.AttributeSet;import android.view.MotionEvent;

超简单实现Android自定义Toast示例(附源码)

Bamboy的自定义Toast,(以下称作"BToast") 特点在于使用简单, 并且自带两种样式: 1)普通的文字样式: 2)带图标样式. 其中图标有√和×两种图标. BToast还有另外一个特点就是: 系统自带Toast采用的是队列的方式,当前Toast消失后,下一个Toast才能显示出来: 而BToast会把当前Toast顶掉, 直接显示最新的Toast. 那么,简单三步,我们现在就开始自定义一下吧! (一).Layout: 要自定义Toast, 首先我们需要一个XML布局. 但