`

自定义 Theme 改变 系统全局样式

阅读更多

转自:http://www.androidworks.com/changing-the-android-edittext-ui-widget

Changing the Android EditText UI Widget

Summary:

This article should be useful to people who want to customize the default UI EditText as well as TextView on the Android platform.  Mostly, I mean the Orange skin that appears to be hard to change.  No matter how many color properties I attempted, I failed.  Then, after inspecting the Android source code, picking apart how they wrote the TextView control (which EditText extends), I realized it was just a skin of NinePatchdrawables set in the background of the underlying View class.  I’ll take you through the steps.

Some background on how it works:

First lets look at the art that Android uses, and how they reference it.  This provides a better understanding of what we need to do on our own.

Look in the <android_sdk>\platforms\android-x.x\data\res\drawable directory.  In this directory, you will notice this file, edit_text.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/textfield_default" />
  <item android:state_window_focused="false" android:state_enabled="false" android:drawable="@drawable/textfield_disabled" />
  <item android:state_pressed="true" android:drawable="@drawable/textfield_pressed" />
  <item android:state_enabled="true" android:state_focused="true" android:drawable="@drawable/textfield_selected" />
  <item android:state_enabled="true" android:drawable="@drawable/textfield_default" />
  <item android:state_focused="true" android:drawable="@drawable/textfield_disabled_selected" />
  <item android:drawable="@drawable/textfield_disabled" />
</selector>

This is the ColorStateList that is default for the EditText background.  This file points to various background resources in each state.  So, from inspection, it appears that we need to create NinePatch art for the following drawable files:

textfield_default.9.png, textfield_disabled.9.png, textfield_pressed.9.png, textfield_selected.9.png, textfield_disabled_selected.9.png, etc…

  •  
  •  
  •  
  •  

So, this is where and how Android’s default EditText gets the Orange look!

Create your own NinePatch skins:

So change the look to your requirements (using gimp, photoshop, or Android’s recommended Draw9Patch tool), in my case I just did a red version of these files.  Place these new png’s in your res/drawable directory.  Now they can be referenced by your very own ColorStateList.

Name them .9.png

  •  
  •  

Create your new ColorStateList

Using the example provided by the default Android edit_text.xml above, create your own version of it, pointing to your own NinePatch files.  This file should live in your res/drawable directory also.  Now you have a valid ColorStateList visible to styles and widgets.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_window_focused="false" android:state_enabled="true"
        android:drawable="@drawable/textfield_default" />
  <item android:state_window_focused="false" android:state_enabled="false"
        android:drawable="@drawable/textfield_disabled_red" />
  <item android:state_pressed="true" android:drawable="@drawable/textfield_pressed_red" />
  <item android:state_enabled="true" android:state_focused="true" android:drawable="@drawable/textfield_selected_red" />
  <item android:state_enabled="true" android:drawable="@drawable/textfield_default" />
  <item android:state_focused="true" android:drawable="@drawable/textfield_disabled_selected_red" />
  <item android:drawable="@drawable/textfield_disabled_red" />
</selector>

Save it as red_edit_text.xml and place in your res/drawable directory.

Point you EditText background to the ColorStateList

In my case, decided to use a theme and style approach to override all EditText boxes in my Application.

From my AndroidManifest.xml application element, set the theme

android:theme="@style/mytheme"

From my theme.xml

<resources> <style name="mytheme" parent="@android:style/Theme" > <item name="android:editTextStyle">@style/red_edittext</item> </style> </resources>

From my styles.xml

<resources>
<style name="red_edittext" parent="@android:style/Widget.EditText">
 	<item name="android:focusable">true</item>
 	<item name="android:focusableInTouchMode">true</item>
 	<item name="android:clickable">true</item>
 	<item name="android:background">@drawable/red_edit_text</item>
 	<item name="android:textColor">@color/state_list</item>
 	<item name="android:gravity">center_vertical</item>
 	<item name="android:textColorHint">@color/default_text_color</item>
 	<item name="android:textColorHighlight">@color/transparent_red</item>
 </style>
 <style name="droiddate_btn" parent="@android:style/Widget.Button">
 	<item name="android:background">@drawable/btn_default_red</item>
 </style>
 </resources>

Results:

So now all EditText boxes should have my new red color instead of the default Orange.  Although this might not be your end goal, just to change an EditText box from orange to red, it allows you to see how Android NinePatchColorStateList , Styles, and Themes can all work together to override and skin any control in Android.  It could obviously be much more dramatic than my example below.  Good luck, here is the result:

Please feel free to comment,
Marcus Williford

mwilliford@androidworks.com

Tags: AndroidAndroid UIColorStateListEditTextStyleTheme

分享到:
评论

相关推荐

    自定义主题改变AlertDialog样式

    本篇文章将深入讲解如何通过自定义`style`来改变`AlertDialog`的样式,包括其背景图片、按钮图片、标题样式、标题栏下方的分割线颜色以及按钮间的分隔线等。 首先,我们需要创建一个新的`style`资源文件,通常在`...

    自定义Android的TabHost控件样式

    通过在`AndroidManifest.xml`或Activity的`onCreate()`方法中指定自定义主题,可以全局改变TabHost的样式。 - 例如,创建一个名为`@style/CustomTabHostTheme`的主题,然后在Activity中使用`setTheme(R.style....

    学习笔记:自定义样式(style)与主题(theme)

    在Android开发中,自定义样式(style)和主题(theme)是两个关键的概念,它们极大地提升了应用的界面设计灵活性和用户体验。自定义样式允许开发者为UI组件设定特定的外观和行为,而主题则可以全局统一应用的视觉风格。...

    android自定义主题

    - 主题通常基于系统默认的主题,例如`Theme.Material3.DayNight`,并在此基础上覆盖自定义属性。 3. **应用主题** - 应用到整个应用:在`AndroidManifest.xml`中,对`&lt;application&gt;`标签设置`android:theme`属性...

    Android改Theme实现夜间模式

    Theme是Android应用的全局样式,它定义了应用的整体外观和感觉,包括颜色、字体、布局等元素。我们可以创建自定义Theme,并在运行时动态切换,从而实现夜间模式和日间模式之间的切换。 1. 创建自定义Theme: 在`...

    android设置全局字体样式

    综上所述,Android全局字体样式的设置涉及多个层面,包括自定义主题、使用自定义字体文件、自定义ViewGroup、利用AppCompat库以及适配不同场景。通过灵活运用这些方法,开发者可以打造出具有独特视觉风格的应用程序...

    Android Style\\Theme动态切换

    Theme是全局的,通常应用于整个应用程序或特定Activity,它定义了一组默认的样式属性。而Style则是更具体的,可以用来定制单个View或Widget的外观。Style通常嵌套在Theme中,但也可单独使用。 1. **定义Theme和...

    Tabhost自定义背景样式

    最后,为了使应用更加美观,我们可能还需要在主题(Theme)中设定全局的TabHost样式,包括未选中和选中状态的背景颜色、文字颜色等。这可以在res/values/styles.xml中完成: ```xml &lt;style name="AppTheme" parent=...

    Theme的用法

    Theme是对整个应用程序或者Activity的全局设定,它会影响到应用中的所有视图或组件。而Style则是对单个视图或者组件的定制,比如一个按钮或文本框的外观。 在Android中,Theme主要定义在res/values/styles.xml文件...

    ActionBar 更换背景、颜色、文字,自定义主题Style

    本篇将详细介绍如何通过自定义Theme和Style来改变ActionBar的背景、颜色以及文字样式,使应用界面更加个性化。 首先,理解Android中的Theme和Style是非常关键的。Theme是全局样式,应用于整个应用程序或特定...

    安卓自定义样式

    如果你想全局改变Dialog的风格,可以创建一个自定义的主题并在AndroidManifest.xml中应用。在`styles.xml`中定义一个新的主题,如`AppTheme.Dialog.Custom`,然后在DialogFragment中设置: ```xml &lt;!-- styles.xml ...

    android 改变字体样式

    在Android开发中,改变字体样式是一项重要的任务,它能让应用程序具有更高的可定制性和用户体验。本文将深入探讨如何在Android应用中实现字体样式的个性化设置,让您的APP更加炫酷。 首先,我们需要理解Android中的...

    antdv ant-design-vue 自定义主题颜色.pdf

    这种方法要求你删除原有的antdv主题样式引用,转而引入自定义的less样式文件。例如: ```javascript // 删除原有antdv主题引用 // import 'ant-design-vue/dist/antd.css'; // import 'ant-design-vue/dist/antd....

    安卓Android源码——theme简单使用示例.zip

    在安卓(Android)开发中,主题(Theme)是一种全局的样式定义,它可以影响应用程序或整个系统的界面外观。主题主要用于统一应用的视觉风格,提供不同设备和用户偏好的适配,以及简化代码,使得开发者不用在每个...

    darkest theme

    5. **操作系统主题**:操作系统如Windows、macOS和Android也提供了深色模式,允许用户全局切换到深色主题,包括系统设置、应用界面等。开发者需要考虑其应用程序如何与这些系统级别的深色主题兼容。 6. **能源效率*...

    Android应用源码之theme.zip

    3. **字体和文字样式**:可以自定义字体文件,并通过的&lt;item name="android:fontFamily"&gt;@font/custom_font来设置全局字体。同时,还可以调整文字大小、颜色、行间距等。 4. **组件样式**:Android主题允许对特定...

    安卓字体使用多语言相关-android设置全局字体样式.rar

    首先,Android系统本身支持多种字体和语言,开发者可以通过修改应用程序的主题(Theme)或在布局文件中直接设置字体来改变文本的显示样式。全局字体样式的设置通常在应用程序的主题中完成,例如在`res/values/styles...

    nebular-theme:修改过样式源码-修改

    例如,可以更改 `$nb-colors` 变量来改变应用的颜色方案,或者调整 `$nb-font-family` 来设置全局字体。 4. **组件扩展与覆盖** 如果需要对 Nebular 的默认组件行为进行扩展或覆盖,可以创建自定义组件并利用 ...

    自定义Dialog示例

    在自定义Dialog时,我们可以改变这些元素的样式、布局和交互方式。 1. **创建自定义Dialog布局** - 在`res/layout`目录下创建一个新的XML布局文件,例如`custom_dialog.xml`。在这个文件中,你可以自由设计Dialog...

Global site tag (gtag.js) - Google Analytics