自定义 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在element中,可以使用全局样式覆盖默认样式。具体步骤如下: 1. 在项目中创建一个样式文件,例如global.css。 2. 在main.js(或者入口文件)中引入该样式文件:`import './global.css'`。 3. 在global.css文件中添加需要覆盖的样式,例如: ```css /* 覆盖el-input的边框颜色 */ .el-input { border-color: #ccc; } ``` 4. 在Vue组件中使用element组件时,该全局样式会自动应用。 注意事项: - 全局样式的优先级比局部样式低,如果需要覆盖局部样式,需要使用更具体的选择器。 - 全局样式会影响所有使用了element组件的地方,因此需要谨慎使用。建议只在必要的情况下使用全局样式。 ### 回答2: element是一款流行的前端UI框架,提供了丰富的组件和样式供开发者使用。在使用element时,可以根据自己的需求进行全局样式自定义。 首先,我们可以在项目中建立一个独立的样式文件(例如element.css或者theme.scss),并在页面中引入该文件。然后我们可以使用CSS的选择器对element的组件进行样式修改。 例如,我们可以使用选择器修改element的按钮的背景颜色: .button { background-color: #f00; } 同样地,我们也可以修改element的表格的样式: .el-table { margin-top: 20px; border: 1px solid #ccc; } 除了使用CSS选择器进行样式修改,element也提供了一些全局的变量供我们进行样式自定义,我们可以在项目中建立一个scss文件,如theme.scss,并在其中定义我们想要修改的全局变量,例如primary-color、success-color等。然后我们在项目的入口文件中引入该scss文件,并且在编译时使用element的默认主题替换掉我们修改的全局变量。 为了方便开发者的样式修改,element还提供了一些mixin函数供使用,例如mixin(el-button)用于修改按钮的样式。我们可以在项目的scss文件中引入element的mixin,并在其中调用mixin函数进行修改。 综上所述,element提供了丰富的全局自定义样式的方法,我们可以通过选择器、全局变量、mixin函数等方式对element的组件进行个性化的样式修改,以满足项目的需求。 ### 回答3: element全局自定义样式是指在使用element组件库开发项目时,我们可以根据自己的需求对element组件的样式进行个性化的定制和修改。 在element组件库中,我们可以通过修改全局样式变量来对组件的样式进行自定义。这些全局样式变量定义在一个名为theme-chalk的SCSS文件中,我们可以通过修改这个文件来改变组件的默认样式。 要修改全局样式变量,我们需要先安装sass编译器,并设置自定义主题的入口样式文件。然后,在这个入口样式文件中,我们可以通过覆盖和修改element组件库中的全局样式变量来改变组件的外观。 例如,如果我们想修改元素的字体大小,我们可以找到theme-chalk文件夹中的_variables.scss文件,在文件中找到对应的变量,如$font-size-base,将它的值修改为我们希望的大小。保存修改后的样式文件,重新编译项目,就可以看到修改后的样式生效。 除了修改全局样式变量外,我们还可以通过添加自定义样式类来实现样式的个性化定制。我们可以为某个element组件添加一个自定义的class属性,并在样式文件中定义该class的样式规则,从而覆盖组件的默认样式。 需要注意的是,全局样式的修改会影响到整个项目中使用到的所有组件,因此在修改样式时要慎重考虑。另外,element组件库也提供了一套基于BEM命名规范的样式类命名方式,我们可以根据需要灵活运用。 总之,element全局自定义样式可以让我们根据自己的需求来改变组件的外观,使得项目更加个性化和美观。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值