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: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-09-06 08:38:20

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&q

android获取联系人示例分享_Android

复制代码 代码如下: package com.homer.phone; import java.util.ArrayList;import java.util.HashMap; import android.app.Activity;import android.database.Cursor;import android.os.Bundle;import android.provider.ContactsContract;import android.provider.ContactsCont

我的Android进阶之旅------&amp;gt;Android自定义窗口标题实例

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

android自定义组件实现方法_Android

本文实例讲述了android自定义组件实现方法.分享给大家供大家参考.具体如下: atts.xml: <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="TopBar"> <attr name="titleText" format="string"/> <

.net自定义事件示例分享

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

Android自定义实现开关按钮代码_Android

我们在应用中经常看到一些选择开关状态的配置文件,做项目的时候用的是android的Switch控件,但是感觉好丑的样子子 个人认为还是自定义的比较好,先上个效果图: 实现过程: 1.准备开关不同状态的两张图片放入drawable中. 2.xml文件中添加代码: <ToggleButton android:id="@+id/switch1" android:layout_width="wrap_content" android:layout_height=&qu

Android自定义照相机倒计时拍照_Android

自定义拍照会用到SurfaceView控件显示照片的预览区域,以下是布局文件: 两个TextView是用来显示提示信息和倒计时的秒数的 相关教程:Android开发从相机或相册获取图片裁剪 Android启动相机拍照并返回图片 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools&qu

Android自定义View实现开关按钮_Android

 前言:Android自定义View对于刚入门乃至工作几年的程序员来说都是非常恐惧的,但也是Android进阶学习的必经之路,平时项目中经常会有一些苛刻的需求,我们可以在GitHub上找到各种各样的效果,能用则用,不能用自己花功夫改改也能草草了事.不过随着工作经验和工作性质,越来越觉得自定义View是时候有必要自己花点功夫研究一下. 一.经过这两天的努力,自己也尝试着写了一个Demo,效果很简单,就是开关按钮的实现. 可能有的人会说这效果so easy,找UI切三张图就完事了,何必大费周折自定义

Android自定义View过程解析_Android

Android自定义的view,主要是继承view,然后实现ondraw这个方法,来进行绘制. 1. 编写自己的自定义view 2. 加入逻辑线程 3. 提取和封装自定义view 4. 利用xml中定义样式来影响显示效果 一.编写自定义的view1.在xml中使用自己的view <!-- 可以使用view的公共属性,例如背景 --> <com.niuli.view.MyView android:layout_width="match_parent" android:l