Android中样式style和主题Theme的使用总结

一.Style的使用

使用style属性可以很方便的抽取一些属性,不用重复写很多相同的属性。

(一)设置属性的集合

1.定义

<style name="TextViewStyle">
        <item name="android:textColor">#000</item>
        <item name="android:textSize">20sp</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_width">match_parent</item>
        <item name="android:background">#8f00</item>
        <item name="android:gravity">center_horizontal</item>
    </style>

    <style name="TextViewStyle1" parent="TextViewStyle">
        <item name="android:textSize">40sp</item>
        <item name="android:background">#8ff0</item>
    </style>

这里parent属性定义的可以是一个style。

2.使用:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        style="@style/TextViewStyle"
        android:text="style" />

    <TextView
        style="@style/TextViewStyle1"
        android:text="style1" />

    <TextView
        style="@style/TextViewStyle"
        android:text="style2"
        android:textSize="40sp" />

</LinearLayout>

3.显示的效果

s1

       这里可以把很多属性写成style来使用,不只是TextView控件标签,其他的任何控件布局标签都可以,如果几个属性相同都是可以抽成style方便调用,后面使用是可以覆盖掉某些属性的。

二.Theme主题的设置

(一)设置全屏,这个应用比较多

1.style设置

<style name="AppTheme" parent="AppBaseTheme">

        <!-- 设置无标题 -->
        <item name="android:windowNoTitle">true</item>
        <!-- 设置全屏 -->
        <item name="android:windowFullscreen">true</item>
    </style>
在AndroidManifest.xml中
<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
。。。
</application>

2.直接设置

       在AndroidManifest.xml中根据需要在或中使用自定义的Android主题方式进行设置。

android:theme="@style/theme_fullScreen"  

3.使用java代码设置

//取消标题  
this.requestWindowFeature(Window.FEATURE_NO_TITLE);或
requestWindowFeature(R.style.AppTheme);  
//全屏  
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);  

无标题的全屏显示:
w1

(二)Theme主题大全

窗口主题设置:(这里主要对于Activity设置,用到系统自动主题内容)
•android:theme=”@android:style/Theme.Dialog” 将一个Activity显示为能话框模式
•android:theme=”@android:style/Theme.NoTitleBar” 不显示应用程序标题栏
•android:theme=”@android:style/Theme.NoTitleBar.Fullscreen” 不显示应用程序标题栏,并全屏
•android:theme=”Theme.Light” 背景为白色
•android:theme=”Theme.Light.NoTitleBar” 白色背景并无标题栏
•android:theme=”Theme.Light.NoTitleBar.Fullscreen” 白色背景,无标题栏,全屏
•android:theme=”Theme.Black” 背景黑色
•android:theme=”Theme.Black.NoTitleBar” 黑色背景并无标题栏
•android:theme=”Theme.Black.NoTitleBar.Fullscreen” 黑色背景,无标题栏,全屏
•android:theme=”Theme.Wallpaper” 用系统桌面为应用程序背景
•android:theme=”Theme.Wallpaper.NoTitleBar” 用系统桌面为应用程序背景,且无标题栏
•android:theme=”Theme.Wallpaper.NoTitleBar.Fullscreen” 用系统桌面为应用程序背景,无标题栏,全屏
•android:theme=”Translucent” 半透明
•android:theme=”Theme.Translucent.NoTitleBar”
•android:theme=”Theme.Translucent.NoTitleBar.Fullscreen”
•android:theme=”Theme.Panel”
•android:theme=”Theme.Light.Panel”

(三)解决Activity切换黑屏、白屏问题:

//1、设置背景图Theme  
<style name="Theme.AppStartLoad" parent="android:Theme">    
    <item name="android:windowBackground">@drawable/ipod_bg</item>    
    <item name="android:windowNoTitle">true</item>    
</style>  

//2、设置透明Theme  
<style name="Theme.AppStartLoadTranslucent" parent="android:Theme">    
    <item name="android:windowIsTranslucent">true</item>   
    <item name="android:windowNoTitle">true</item>    
</style>  

Theme1 程序启动快,界面先显示背景图,然后再刷新其他界面控件。给人刷新不同步感觉。
Theme2 给人程序启动慢感觉,界面一次性刷出来,刷新同步。

三.设置窗体透明度,昏暗度,背景模糊处理:

(一)透明度

WindowManager.LayoutParams lp=getWindow().getAttributes();
 lp.alpha=0.5f;
 getWindow().setAttributes(lp);
alpha在0.0f到1.0f之间。

(二)昏暗度

WindowManager.LayoutParams lp=getWindow().getAttributes();
   lp.dimAmount=0.5f;
  getWindow().setAttributes(lp);
   getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
dimAmount在0.0f和1.0f之间。

(三)背景模糊

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
      WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
View设置View view=...
view.getBackground().setAlpha(100);//0~255透明度值 ,0为完全透明,255为不透明

      上面的知识,不一定说都要会用,知道常用的就可以了,有些不常用的可以查阅后使用,但是使用的方式是必须要知道了。

  • 29
    点赞
  • 72
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

峥嵘life

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值