在项目开发中经常遇到这样的问题,在很多的Activity中都会有标题栏,而这个标题栏通常拥有类似的效果,比如下面的截图所示。那么你需要统一控制标题栏中的文字的大小,字体的颜色等等。方便修改和维护。
1.在res/values/styles.xml
中为每个控件编写style:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<!-- 页面头部 返回按钮样式 -->
<style name="header_button_back" parent="@android:style/TextAppearance.Widget.Button">
<item name="android:textSize">@dimen/header_text_size</item>
<item name="android:textColor">@color/header_button_text_color</item>
<item name="android:layout_height">@dimen/header_button_height</item>
<item name="android:layout_width">@dimen/header_button_back_width</item>
<item name="android:background">@drawable/btn_back_selector</item>
<item name="android:text">返回</item>
<item name="android:layout_marginLeft">8dip</item>
<item name="android:paddingLeft">8dp</item>
<item name="android:layout_gravity">center</item>
</style>
<!-- 页面头部 标题样式 -->
<style name="header_text_view" parent="@android:style/TextAppearance.Widget.TextView">
<item name="android:textSize">@dimen/header_title_text_size</item>
<item name="android:textColor">@color/header_button_text_color</item>
<item name="android:layout_height">@dimen/header_button_height</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_weight">1</item>
<item name="android:gravity">center</item>
<item name="android:text">标题</item>
</style>
<!-- 页面头部 操作按钮样式 -->
<style name="header_button_operate" parent="@android:style/TextAppearance.Widget.Button">
<item name="android:textSize">@dimen/header_text_size</item>
<item name="android:textColor">@color/header_button_text_color</item>
<item name="android:layout_height">@dimen/header_button_height</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_marginRight">5dp</item>
<item name="android:layout_marginLeft">9dip</item>
<item name="android:text">操作</item>
<item name="android:layout_gravity">center</item>
</style>
<!-- 页面头部 容器样式 -->
<style name="header_linear_layout">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">@dimen/header_height</item>
<item name="android:background">@drawable/bg_navbar_no_line</item>
<item name="android:gravity">center</item>
</style>
</resources>
2.在activity的布局文件中利用这些style:
<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"
tools:context=".MainActivity" >
<!-- HeadBar
这样的话,你可以很容易地定义一个titlebar的样式,方便使用和统一修改。
如果需要自定义可以在下面设置自己的属性,能够覆盖style中的属性,比如你可以设置layout_margin等等,能够覆盖style中的layout_margin
-->
<LinearLayout
style="@style/header_linear_layout"
android:layout_alignParentTop="true" >
<Button
android:id="@+id/btn_back"
style="@style/header_button_back"
/>
<TextView
style="@style/header_text_view"
android:text="我的个人中心" />
<Button
android:id="@+id/btn_operate"
style="@style/header_button_operate"
android:visibility="invisible" />
</LinearLayout>
</RelativeLayout>
在你需要Titlebar的Activity的布局文件中使用上面的布局,就可以达到很方便控制所有控件属性的目的了。当然这也不妨碍你自定义某个控件的属性,因为你自定义的属性能够覆盖style中的属性。
上面style文件中用到的dimen这里也贴出来:res/values/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="header_height">50dip</dimen>
<dimen name="header_button_height">50dip</dimen>
<dimen name="header_text_size">16sp</dimen>
<dimen name="header_title_text_size">22sp</dimen>
<dimen name="header_button_back_width">60dp</dimen>
<dimen name="header_tab_width">80dp</dimen>
<dimen name="header_tab_group_width">162dp</dimen>
</resources>
同样的color:res/values/colors.xml
:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="act_main_item_text_color">#505355</color>
<color name="header_button_text_color">#e5e9ec</color>
</resources>
- 大小: 49.7 KB
分享到:
相关推荐
Android Theme和Styles内部定义解析.txt
将ini文件存在android项目中assets文件夹中,以便于软件开启时自动读取ini系统配置文件。
Android Studio 在xml文件中设置界面布局1、xml文件中给输入框editText修改下划线颜色2、设置button按钮为圆角及修改按钮颜色3、运用约束布局时预览效果和运行效果不一致 写一写今天困扰我很久的问题:如何给...
作为使用zeplin来弥合设计和开发之间差距的支持者,我经常感到痛苦的一个因素是,将文本样式从zeplin导出到android的支持度很低。虽然技术上是可行的,但有些基本功能不能正常工作或没有更新,请记住这些年来我们在...
Debatekeeper is an Android app that times speeches in debates and rings bells automatically at the correct times. It supports most parliamentary styles of debating, including British Parliamentary, ...
It will generate all necessary nine patch assets plus associated XML drawables and styles which you can copy straight into your project. Use it online here: ...
android XML解析的三种方法源代码
android项目中经常会接触到加载网络图片的情况,这里向大家展示几种最为常见访问网络图片的几种方式.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
一个android主题设置的demo,注意自己定义属性的时候要写在attrs.xml中然后才可以在styles.xml中使用
该项目包含适用于Android设备的不同基准测试和使用不同线程风格实现的程序。 除“ ImageBlurExplPowerM”外,这些程序均未设计为自动与电源监视器一起运行。 为了使他们能够使用电源监视器,应该向其中添加一个“ ...
Do not forget to add elevation with android:elevation="**dp" KenBurns Using https://github.com/flavioarfaria/KenBurnsView ... Inspirations Exploration of some new styles and approaches on ...
<item name="android:button">@drawable/selector</item>//这个selector.xml中就是对应不同状态的CheckBox的背景图片 3:在selector.xml <selector xmlns:android=...
主题(theme)应用到整个Activity和Application的样式,当设置好主题后,Activity或整个程序中的视图都将使用主题中的属性,当主题和样式中的属性发生冲突时,样式的优先级要高于主题。 自定义主题代码如下(styles....
Android WatchFace Sample A simple sample that demonstrates watch faces and complications for Wear 2.0. Introduction This sample demonstrates how to create watch faces for Wear. The analog watch face ...
Codeview is a android library tha lets you preview code in webview very easy and simple with highlighs and colors. With styles to chooses and language. Also you can inject ...
Android StyleableToast An Android library that takes the standard Android Toast to the next level with many styling options that gives your app and user experience an extra unique feeling! Style your ...
Includes three styles (incl. Material Design Snackbar) Supports API Level >= 8 (using nineoldandroids) I18N (about ~100 languages) Screenshots pre KitKat KitKat Lollipop and later Usage ...
Android Resource Navigator A Google Chrome Extension providing enhanced resource navigation...Quickly open the Android framework styles or themes XML via an always available browser action button. Frame
Android Wear and Android Auto support Playing queue in notification (Xposed) Changelog Changelog is available here Credits CyanogenMod's Eleven Music Player TimelyTextView MultiViewPager PlayPaus