android自定义style实现2

来源:互联网 发布:win32 sdk编程 编辑:程序博客网 时间:2024/06/06 06:36

1、在res/values/styles定义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="CodeFont" parent="@android:style/TextAppearance.Medium">        <item name="android:layout_width">wrap_content</item>        <item name="android:layout_height">wrap_content</item>        <item name="android:typeface">monospace</item>    </style>    <style name="CodeFont.Red">        <item name="android:textColor">#00FF00</item>    </style>    <style name="CodeFont.Red.Big">        <item name="android:textSize">30sp</item>    </style>    <color name="custom_theme_color">#b0b0ff</color>    <!-- 表示Application的一个主题 -->    <style name="CustomTheme" parent="android:Theme.Light">        <item name="android:windowBackground">@color/custom_theme_color</item>        <item name="android:colorBackground">@color/custom_theme_color</item>    </style></resources>

2、在布局文件中引用主题

<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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <TextView        style="@style/CodeFont.Red.Big"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" /></RelativeLayout>



0 0
原创粉丝点击