一、样式(Style)
在写布局时,当多个视图有不少相同的属性时,可以将这些相同的属性放在一起在styles.xml中自定义为一个style。在布局文件中 使用style=”@style/style_name”统一引用。
可以把样式理解为多个视图属性的集合。
一、样式的作用
复用视图标签属性,防止布局文件累赘。
二、样式的目标
针对窗口中的某些视图。
三、样式的定义与继承
在styles.xml定义样式:
<resources>
<style name="textstyle">
<item name="android:layout_width">fill_parent</item>
<item name="android:gravity">center</item>
</style>
<style name="subtextstyle" parent="textstyle">
<item name="android:layout_width">wrap_content</item>
<item name="android:textColor">#000000</item>
</style>
<resources>
在布局文件中引用:
<TextView
style="@style/subtextstyle"
android:text="@string/hello" />
其中name为我们定义的样式名字,便于我们对样式的引用,parent为对父样式的继承,可以将父样式的标签属性继承过来,可以在继承中对标签属性覆写。
我们也可以引用/继承Android系统已定义的一些系统样式:style=”@android:style/xxxx”
样式的标签属性的优先级:就近原则
二、主题(Theme)
主题的本质也是样式style,作用也是复用视图的标签属性,在styles.xml中定义,可继承/覆写,但是在manifest.xml中引用。
一、主题的目标
针对整个应用Application或某个Activity的界面
二、主题的定义与使用示例:
在styles.xml定义主题:
<style name="CustomTheme" parent="android:Theme.Light">
<item name="android:windowBackground">#FFFFFF</item>
</style>
在manifest.xml中引用主题
<application android:theme="@style/CustomTheme">
...
<activity android:theme="@style/CustomTheme">
...
主题中的标签属性的优先级:就近原则
(窗口视图的属性>窗口中的视图style>activity中的主题的标签属性>application中的主题的标签属性)系统常用主题:
- @android:style/Theme.Light.NoTitleBar : 没有标题栏
- @android:style/Theme.Light.NoTitleBar.Fullscreen : 全屏
- @android:style/Theme.Dialog : 对话框
使用代码设置Activity主题:this.setTheme(R.style.CustomTheme)
三、使用主题进行夜间/白天模式的动态转换
主要通过对Activity主题的设置来实现
效果图:
一、设置主题的属性
根据主题的不同,设置不同属性给布局文件的视图来使用。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="titleBarColor" format="color" />
<attr name="textColor" format="color" />
<attr name="textBackground" format="color" />
<attr name="imageValue" format="reference"/>
</resources>
二、设计两个主题
一个白天,一个黑夜,为防止Activity的主题被覆盖,使这两个主题继承原主题:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="DayTheme" parent="AppTheme">
<item name="titleBarColor">@color/day_titlebar</item>
<item name="textColor">@color/day_text</item>
<item name="textBackground">@android:color/white</item>
<item name="imageValue">@drawable/night</item>
</style>
<style name="NightTheme" parent="AppTheme" >
<item name="titleBarColor">@color/night_titlebar</item>
<item name="textColor">@android:color/white</item>
<item name="textBackground">@android:color/black</item>
<item name="imageValue">@drawable/day</item>
</style>
三、在Activity中进行逻辑判断并加载主题
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(MyApplication.appConfig.isNighTheme()){
this.setTheme(R.style.NightTheme);
isNight = true;
}else{
this.setTheme(R.style.DayTheme);
isNight = false;
}
setContentView(R.layout.activity_main);
}
四、实现更换主题并带原意图重启Activity
通过使用动画来重启Activity防止闪烁
public void changeTheme(View view) {
//Toast.makeText(this,"123",Toast.LENGTH_SHORT).show();
if(isNight){
MyApplication.appConfig.setNightTheme(false);
}else{
MyApplication.appConfig.setNightTheme(true);
}
Intent intent = getIntent();
overridePendingTransition(R.anim.in_anim, R.anim.out_anim);
startActivity(intent);
finish();
}
本文介绍了安卓开发中样式和主题的使用,详细阐述了样式的定义、继承以及作用,强调了样式的优先级规则。接着讲解了主题的创建与应用,特别讨论了如何利用主题实现应用的夜间/白天模式动态转换,并提供了具体的代码实现,包括设计两种主题、在Activity中切换主题以及平滑重启Activity避免闪烁的方法。
4556

被折叠的 条评论
为什么被折叠?



