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

所属分类: 软件编程 / Android 阅读数: 821
收藏 0 赞 0 分享

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>

更多精彩内容其他人还在看

Android异常 java.lang.IllegalStateException解决方法

这篇文章主要介绍了Android异常 java.lang.IllegalStateException解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Split()字符串分割特殊用法案例详解

本文通过案例的形式给大家详细介绍了android中split()字符串分割特殊用法的知识,非常不错具有参考借鉴价值,感兴趣的朋友参考下
收藏 0 赞 0 分享

Android仿新浪微博启动界面或登陆界面(1)

这篇文章主要为大家详细介绍了Android仿新浪微博启动界面或登陆界面的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿新浪微博oauth2.0授权界面实现代码(2)

这篇文章主要为大家详细介绍了Android仿新浪微博oauth2.0授权界面实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android开发中使用sqlite实现新闻收藏和取消收藏的功能

本篇文章主要介绍了sqlite实现新闻收藏和取消收藏功能,主要涉及到oracle数据库方面的内容,对于Android开发sqlite实现收藏和取消功能感兴趣的朋友可以参考下本文
收藏 0 赞 0 分享

Android仿新浪微博分页管理界面(3)

这篇文章主要为大家详细介绍了Android仿新浪微博分页管理界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android UI自定义ListView实现下拉刷新和加载更多效果

这篇文章主要介绍了Android UI自定义ListView实现下拉刷新和加载更多效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android—基于微信开放平台v3SDK开发(微信支付填坑)

这篇文章主要介绍了Android—基于微信开放平台v3SDK开发(微信支付填坑),具有一定的参考价值,有需要的可以了解一下。
收藏 0 赞 0 分享

Android仿新浪微博自定义ListView下拉刷新(4)

这篇文章主要为大家详细介绍了Android仿新浪微博自定义ListView下拉刷新,重点介绍了Adapter的详细代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android控件之使用ListView实现时间轴效果

这篇文章主要介绍了Android基础控件之使用ListView实现时间轴效果的相关资料,本文是以查看物流信息为例,给大家介绍了listview时间轴的实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多