首页 文章

如何为android项目创建主题和xml样式

提问于
浏览
4

如何在android 4.2中创建样式主题文件 . 如何将这个主题应用于android项目的所有活动 . 如何将此样式和主题设置为多个屏幕?

http://developer.android.com/guide/topics/ui/themes.html>

1 回答

  • 6

    在应用程序的res / values目录中创建名为styles.xml的文件 . 添加根节点 . 对于每个样式或主题,添加具有唯一名称的元素,以及可选的父属性 . 该名称稍后用于引用这些样式,父级指示要继承的样式资源 . 在元素内部,在一个或多个元素中声明格式值 . 每个都使用name属性标识其style属性,并在元素内定义其样式值 . 然后,您可以从其他XML资源,清单或应用程序代码中引用自定义资源 .

    A theme is a style applied to an entire Activity or application,

    <style name="MyTheme" parent="android:Theme.Light">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@color/translucent_red</item>
        <item name="android:listViewStyle">@style/MyListView</item>
    </style>
    
    <style name="MyListView" parent="@android:style/Widget.ListView">
        <item name="android:listSelector">@drawable/ic_menu_home</item>
    </style>
    

    要定义样式,请将XML文件保存在项目的/ res / values目录中 . XML文件的根节点必须是 .

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="text">
            <item name="android:padding">4dip</item>
            <item name="android:textAppearance">?android:attr/textAppearanceLarge</item>
            <item name="android:textColor">#000000</item>
        </style>
        <style name="layout">
            <item name="android:background">#C0C0C0</item>
        </style>
    </resources>
    

    在AndroidManifest.xml中,将主题应用于要设置样式的活动:

    <activity
            android:name="com.myapp.MyActivity"
            ...
            android:theme="@style/MyTheme"
            />
    

相关问题