android定义theme和style

来源:互联网 发布:ipa 知乎 编辑:程序博客网 时间:2024/05/17 06:41

方便地定义显示效果,不用每次定义组件时重复定义属性值,在Android系统中引入了主题Theme和样式Style的概念。Theme是针对窗体级别的,而Style是针对组件级别的,他们都可以通过Style.xml资源文件进行设置。主题和样式都可以通过继承实现重用。

    ThemeStyle资源都存放在res/values/style.xml文件中,通过style标签中的name属性定义其引用名称,通过parent属性指定被继承的资源。在style标签内部,申明了一个或者多个item标签,item标签中的name属性指定其定义的具体属性名,并且在item标签内部指定具体数值。代码如下:

<style name =”btnStyle” parent = ”@style/baseStyle”>

    <item name = ”android:textColor”>#826455</item>

    <item name = ”android:textSize”>28dp</item>

</style>

 

实例:

 

·res/values/style.xml

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

<resources>

    <style name="baseStyle">

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

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

    </style>

   

    <style name="btnStyle" parent="@style/baseStyle">

       <item name="android:textColor">#826455</item>

       <item name="android:textSize">28dp</item>

    </style>

   

    <style name="customTheme" parent="android:Theme.Black"> 

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

       <item name="android:textSize">14sp</item> 

       <item name="android:textColor">#FFFF0000</item> 

    </style> 

</resources>

 

·res/layout/main.xml

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

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

    android:orientation="vertical"

    style="@style/baseStyle">

    <Button style="@style/btnStyle"

       android:text="@string/txt"/>

    <TextView style="@style/baseStyle"

       android:text="@string/txt"/>

</LinearLayout>

 

·AndroidManifiest.xml

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

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

    package="com.uppowerstudio.chapter3.style" android:versionCode="1"

    android:versionName="1.0">

    <application android:icon="@drawable/icon" android:label="@string/app_name">

       <activity android:name=".MainActivity" android:label="@string/app_name"

           android:theme="@style/customTheme">

           <intent-filter>

              <action android:name="android.intent.action.MAIN" />

              <category android:name="android.intent.category.LAUNCHER" />

           </intent-filter>

       </activity>

    </application>

   

    <uses-sdk android:minSdkVersion="7" />

</manifest>

 

 

总结:主题和样式的引用方式很简单,就是给属性style赋值。值为在res/values文件中定义的style的name。Values目录下的style.xml文件中可以定义很多个不同的style,他们通过name属性来进行区分。

Style可以继承已有的style。Style中主要是各种属性的定义。组件和窗体引用之后,就会具有在style中定义的属性。

组件在使用style时,直接style=”@style/name”就行。如果是窗体,需要在AndroidManifiest.xml文件中通过android:theme = ”@style/name”来进行修改






在Web开发中,Html负责内容,CSS负责表现。同样,在Android开发中,可以使用Theme、Style+UI组件的方式实现内容和形式的分离。

Style是针对窗体元素级别的,改变指定控件或者Layout的样式

Theme是针对窗体级别的,改变窗体样式;

style演示

定义一个styles,在res/values/目录下建立styles.xml

    <style name="mystyle" parent="AppBaseTheme">        <item name="android:textSize">18sp</item>        <item name="android:textColor">#ff0000</item>    </style>

<item name="属性">属性值</item>

1、在布局文件中引用style

<TextView        style="@style/mystyle"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />

2、在程序中设置style

 text.setTextAppearance(this, R.style.mystyle);

效果:

 Theme演示

1、在manifest当中设置主题

theme的style可以定义在styles.xml中,也可以单独定义在自己新建的themes.xml

复制代码
<?xml version="1.0" encoding="utf-8"?><resources xmlns:android="http://schemas.android.com/apk/res/android">      <style name="myTheme" parent="AppBaseTheme">        <item name="android:windowNoTitle">true</item>  <item name="android:windowFullscreen">?android:windowNoTitle</item>    </style></resources>
复制代码

如果整个工程用一个主题就在application 标签中定义

android:theme="@style/myTheme"

 其他属性:可以在sdk查看:..\sdk\platforms\android-7\data\res\values\themes.xml

android:windowBackground

android:windowIsFloating

android:windowIsTranslucent

android:windowContentOverlay

android:windowAnimationStyle

android:backgroundDimEnabled


0 0
原创粉丝点击