Android 使用主题资源

来源:互联网 发布:大学软件开发专业 编辑:程序博客网 时间:2024/05/21 09:16

1 与样式资源类似 Android的主题资源的xml文件通常放在/res/values 目录下,同样以<resource ../> 作为根元素,使用<style,,/>来定义主题

2 主题资源与样式资源的区别在于:

     1  主题不能作用于单个的View组件,主体应该对整个应用的所有Activity起作用,或对指定的Activity起作用

     2 主体定义的格式应该是改变窗口外观的格式

3 代码:

    xml文件

    

<resources>    <!--        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="my_style">        <item  name="android:windowNoTitle">true</item>        <item name="android:windowFullscreen">true</item>        <item name="android:windowFrame">@drawable/window_border</item>        <item name="android:windowBackground">@drawable/star</item>    </style></resources>
    2  Activity

    

package com.example.themetest;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setTheme(R.style.my_style);//设置主题setContentView(R.layout.activity_main);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
   如果想让自己的应用中的所有窗口都使用该主题,只要为Application元素添加android:theme属性 

   

    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/my_style" >        <activity            android:name="com.example.themetest.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>
  如果只想让程序中的某一个窗口使用该主题 ,可以修改该<Activity.../ >使用<android:theme>

     

   <activity            android:name="com.example.themetest.MainActivity"            android:label="@string/app_name"             <!-- 修改该Activity显示主题 -->            android:theme="@style/my_style">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>


0 0
原创粉丝点击