android开发中如何自定义标题栏

android开发中如何自定义标题栏,第1张

原装的Android标题栏配色比较单调,就是黑色的一坨,现在假设你的软件需要独自添加标题栏,这样不仅美观而且可以将进度条等加进去,如何实现:

方法一、在你的那张Activity中onCreate方法中加上下面代码:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE)

setContentView(R.layout.main) //软件activity的布局

但是新的问题又来了,这样是无法深层的定制标题栏的,比如原有的高度和背景都没有发生变化,那有没有好的方法呢?答案是有的、

方法二:

因此先定义一个style,若修改背景请修改android:windowTitleBackgroundStyle

若修改标题栏高度,请修改android:windowTitleSize

<?xml version="1.0" encoding="utf-8"?>

<resources xmlns:android="http://schemas.android.com/apk/res/android">

<style name="CustomWindowTitleBackground">

<item name="android:background">#565656</item>

</style>

<style name="test" parent="android:Theme">

<item name="android:windowTitleSize">50dp</item>

<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>

</style>

</resources>

在程序的android_manifest.xml中对应activity中添加属性android:theme = "@style/test"

就可以了

<activity android:name=".Test"

android:theme = "@style/test" //就在这里

>

</activity>

之后借助于设置自定义的标题栏xml文件,就可以自定义标题栏布局了

我们做应用的时候经常想有自己的标题栏,在android平台示例:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE)

getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,

R.layout.custom_title_1)

custom_title_1为自己定义的标题栏风格的xml文件索引。

当然仅仅这样我们是去不掉系统的标题的,仅仅是在系统标题栏下面增加了一条自己的(客户,注:android

UI系统是C/S模式)标题,怎么用自己的标题栏覆盖系统的呢?下面我们设计自己风格的标题。

两种方式哦

1:

首先建立自己的styles.xml文件,如下:

<?xml version="1.0" encoding="utf-8"?>

<!-- Copyright (C) 2007 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the

"License")

you may not use this file except in compliance with the

License.

You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in

writing, software

distributed under the License is distributed on an "AS

IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either

express or implied.

See the License for the specific language governing

permissions and

limitations under the License.

-->

<resources>

<!-- Base application theme is the default theme.

-->

<style name="Theme" parent="android:Theme">

</style>

<!-- Variation on our application theme that forces a

plain

text style. -->

<style name="Theme.PlainText">

<item

name="android:textAppearance">@style/TextAppearance.Theme.PlainText</item>

</style>

<!-- Variation on our application theme that has a

black

background. -->

<style name="Theme.Black">

<item

name="android:windowBackground">@drawable/screen_background_black</item>

</style>

<!-- A theme for a custom dialog appearance.

Here we use an ugly

custom frame. -->

<style name="Theme.CustomDialog"

parent="android:style/Theme.Dialog">

<item

name="android:windowBackground">@drawable/filled_box</item>

</style>

<!-- A theme that has a wallpaper background.

Here we explicitly specify

that this theme is to inherit from the

system's wallpaper theme,

which sets up various attributes

correctly. -->

<style name="Theme.Wallpaper"

parent="android:style/Theme.Wallpaper">

<item

name="android:colorForeground">#fff</item>

</style>

<!-- A theme that has a translucent background.

Here we explicitly specify

that this theme is to inherit from the

system's translucent theme,

which sets up various attributes

correctly. -->

<style name="Theme.Translucent"

parent="android:style/Theme.Translucent">

<item

name="android:windowBackground">@drawable/translucent_background</item><!--

@drawable/translucent_background -->

<item

name="android:windowNoTitle">true</item>

<item

name="android:colorForeground">#fff</item>

</style>

<!-- Variation on our application theme that has a

transparent

backgroundthis example completely

removes the background,

allowing the activity to decide how to

composite. Also here we

force the translucency ourself rather

than making use of the built-in

translucent theme. -->

<style name="Theme.Transparent">

<item

name="android:windowIsTranslucent">true</item><!-- true -->

<item

name="android:windowAnimationStyle">@android:style/Animation.Translucent</item>

<item

name="android:windowBackground">@drawable/transparent_background</item>

<item

name="android:windowNoTitle">true</item>

<item

name="android:colorForeground">#fff</item>

</style>

<style name="TextAppearance.Theme.PlainText"

parent="android:TextAppearance.Theme">

<item

name="android:textStyle">normal</item>

</style>

<style name="ImageView120dpi">

<item

name="android:src">@drawable/stylogo120dpi</item>

<item

name="android:layout_width">wrap_content</item>

<item

name="android:layout_height">wrap_content</item>

</style>

<style name="ImageView160dpi">

<item

name="android:src">@drawable/stylogo160dpi</item>

<item

name="android:layout_width">wrap_content</item>

<item

name="android:layout_height">wrap_content</item>

</style>

<style name="ImageView240dpi">

<item

name="android:src">@drawable/stylogo240dpi</item>

<item

name="android:layout_width">wrap_content</item>

<item

name="android:layout_height">wrap_content</item>

</style>

<style

name="WindowTitleBackground_my">

<item

name="android:background">@drawable/title_bar</item>

</style>

<style

name="theme_mybackground">

<item

name="android:windowFullscreen">true</item>

<item

name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground_my</item>

</style>

</resources>

此文件是我从google给的示例代码里面拷贝出来的,红色是为去掉系统标题栏新添加的,我们定义了自己标题栏的背景图片(或者定义颜色也可),theme_mybackground

是覆盖了系统风格,系统默认的android:windowFullscreen为false,修改系统默认的windowTitleBackGroundStyle为自己的风格Drawable,OK,下面比较关键的来了,在你的应用程序的Activity的超类里面,在用super.onCreate(savedInstanceState)之前写一句代码如下:

setTheme(R.style.theme_mybackground)

super.onCreate(savedInstanceState)

//do something.

或者加入AndroidManifest.xml

OK,测试运行之

2:

直接在代码里面写

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

WindowManager.LayoutParams.FLAG_FULLSCREEN)

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE)

getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,

R.layout.custom_title_1)

custom_title_1为我们自己的标题栏风格,

测试运行之

转载

.定义标题栏布局

2.自定义TitleActivity控制标题栏按钮监听

3.在TitleActivity中实现标题栏以下内容切换

首先定义标题栏

[html] view plain copy print?

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/layout_titlebar"

android:layout_width="match_parent"

android:layout_height="52dp"


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11383571.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存