美文网首页
Android 主题和样式

Android 主题和样式

作者: _春夏秋冬 | 来源:发表于2018-08-23 14:04 被阅读0次

    R.style https://developer.android.com/reference/android/R.style
    样式和主题 https://developer.android.com/guide/topics/ui/themes
    Style resource(有关 XML 中样式和主题语法的详细信息) https://developer.android.com/guide/topics/resources/style-resource
    R.attr https://developer.android.com/reference/android/R.attr

    <!-- 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>
    

    使用:在清单文件的<application>或者<activity>节点里添加

    android:theme="@style/AppTheme"
    

    这是AS每次自动为我们添加的代码。其实Android里面的主题有很多种。

    第一讲:主题来源

    • 系统内置预定义主题
    • 兼容包里面预定义主题

    1-系统内置预定义主题

    1)分类

        系统自带主题:
        API 1:
        Theme 根主题 灰色标题黑色内容  
        Theme.Black 背景黑色
        Theme.Wallpaper 以桌面墙纸为背景.希望用户选择的壁纸出现在窗口后面的主题(API级别10或更低)。
        Theme.Light 背景白色
        Theme.Translucent 透明背景
        Theme.Panel 平板风格
       Theme.Dialog 对话框风格
    
        API 11: 3.0
        Theme.Holo Holo根主题
        Theme.Holo.Black Holo黑主题
        Theme.Holo.Light Holo白主题
    
        API 14: 4.0
        Theme.DeviceDefault 设备默认根主题
        Theme.DeviceDefault.Black 设备默认黑主题
        Theme.DeviceDefault.Light 设备默认白主题
    
        API 21: (网上常说的 Android Material Design 就是要用这种主题)
        Theme.Material Material根主题
        Theme.Material.Light Material白主题
    
    
        兼容包v7中带的主题:
        Theme.AppCompat 兼容主题的根主题
        Theme.AppCompat.Black 兼容主题的黑色主题
        Theme.AppCompat.Light 兼容主题的白色主题
    

    根主题包含很多样式:

        android:Theme包含一下很多样式
        <!-- Text styles -->
        <!-- Button styles -->
        <!-- List attributes -->
        <!-- @hide -->
        <!-- Gallery attributes -->
        <!-- Window attributes -->
        <!-- Dialog attributes -->
        <!-- AlertDialog attributes -->
        <!-- Toast attributes -->
        <!-- Panel attributes -->
        <!-- Scrollbar attributes -->
        <!-- Text selection handle attributes -->
        <!-- Widget styles -->
        <!-- Preference styles -->
        <!-- Search widget styles -->
        <!-- Action bar styles -->
        <!-- Floating toolbar styles -->
    

    2)语法

    两种写法:

      <style name="AppTheme" parent="android:Theme.Holo.Light"/>
      等价于
      <style name="AppTheme" parent="@android:style/Theme.Material.Light"/>
    

    2-兼容包里面预定义主题

    1.只有一种。Activity要继承AppCompatActivity才行。这是必要条件。
    2.语法:

      <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"/>
    





    第二讲:Android全屏

    XML控制

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- 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="AppTheme.Fullscreen" parent="AppTheme">
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>
    

    代码控制

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 即隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
    }
    





    第三讲:

    @ 和 ? 的区别?

    • 使用@表示使用固定的style,而不会跟随Theme改变,该style可以在对应的style.xml中找到。
    • 使用?表示从Theme中查找引用的资源名,这个Google叫做预定义样式,用在多主题的场景,属性值会随着Theme而改变。(?需要和attr配合使用)

    style 使用的两种场景?

    <!--使用自定义的style-->
    @style/Widget.AppCompat.ProgressBar.Horizontal
    <!--使用系统自带的style-->
    @android:style/Widget.ProgressBar.Horizontal
    

    attr 使用的两种场景

    <!--使用自定义的  ,下面两种方式等效-->
    "?attr/属性"
     "?属性"
    <!--使用系统自带的 ,下面两种方式等效-->
    "?android:属性"
    "?android:attr/属性"
    

    当引用系统自带的style和attr时

    "@android:style/主题"和"@style/android:主题"等同
    "?android:attr/属性"和"?attr/android:属性"等同
    

    参考文章:

    1. 总结一下Android中主题(Theme)的正确玩法
    2. Android中 @和?区别以及?attr/与@style/等的区别
    3. Android中XML的命名空间、自定义属性
    4. 如何理解Android中的xmlns

    相关文章

      网友评论

          本文标题:Android 主题和样式

          本文链接:https://www.haomeiwen.com/subject/tawpiftx.html