Android 内建Theme的使用

来源:互联网 发布:上海关键词优化公司 编辑:程序博客网 时间:2024/06/11 11:51

新建Android项目默认会有主题,不同的版本默认样式可能不同。开发中常用到的主题有哪些做下总结,以供参考。

1.关于默认主题

如果application 或者activity没有指定特定的主题 若API的等级在10以下默认为 @android:style/Theme 如果 API的等级在11-13之间 默认为@android:style/

Theme.Holo 在API等级在14的情况下默认为 @android:style/Theme_DeviceDefault

一般来说不会直接使用一种主题作为application或者activity的主题。

2.关于如何使用内建主题

a)在manifest中设置,application标签或者activity的theme属性例如

    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@android:style/Theme" >        <activity            android:name=".ThemeDemoActivity"            android:label="@string/title_activity_theme_demo"            android:theme="@android:style/Theme.NoTitleBar" >        </activity>        <activity            android:name=".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>

其中applicatioin设置的theme会应用到所有的子标签上,而在activity上设置theme只会作用于单个activity

b)在activity代码中动态设置

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setTheme(android.R.style.Theme_NoTitleBar);setContentView(R.layout.activity_theme_demo);}

其中setTheme需要在任何View的初始化前调用。一般在setContentView(View)下或者inflate(view,ViewGroup)前

3.常用的Theme示例

a)默认的主题,无论是那种API等级都会有状态栏,标题栏,当前为示例为API19



b)Theme.NoTitleBar


c)Theme.NoTitleBar.Fullscreen



d)Theme_Holo_Dialog_NoActionBar


0 0
原创粉丝点击