简体   繁体   English

Android 风格和主题的区别

[英]Android styles and themes difference

According to the documantation: A theme is a style applied to an entire Activity or application, rather than an individual View .根据文档:主题是应用于整个 Activity 或应用程序的样式,而不是单个 View

But when I added theme to my view (a textview) in xml it was compiled and working.但是当我在 xml 中将主题添加到我的视图(文本视图)时,它被编译并工作。 I thought that when I want to add to an individual View I need to use "style".我认为当我想添加到单个视图时,我需要使用“样式”。

<TextView
android:id="@+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/TestTheme"
android:textSize="25dp"
android:text="TextView" />

Does anybody can explain to me?有人可以向我解释吗?

A style is a collection of attributes that specify the look and format for a View.样式是指定视图外观和格式的属性集合。 A style can specify attributes such as height, padding, font color, font size, background color, and much more.样式可以指定诸如高度、填充、字体颜色、字体大小、背景颜色等属性。

For example, by using a style, you can take this layout XML:例如,通过使用样式,您可以采用此布局 XML:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#00FF00"
    android:typeface="monospace"
    android:text="@string/hello" />

And turn it into this:并将其变成这样:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="@style/CodeFont"
    android:text="@string/hello" />

The attributes related to style have been removed from the layout XML and put into a style definition called CodeFont , which is then applied using the android:textAppearance attribute.与样式相关的属性已从布局 XML 中删除,并放入名为CodeFont的样式定义中,然后使用 android:textAppearance 属性应用该定义。

To create the CodeFont style, save an XML file in the res/values/ directory of your project.要创建CodeFont样式,请在项目的 res/values/ 目录中保存一个 XML 文件。 Here is an example:下面是一个例子:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CodeFont" parent="@android:style/TextAppearance.Medium">
        <item name="android:textColor">#00FF00</item>
        <item name="android:typeface">monospace</item>
    </style>
</resources>

This example style can be referenced from an XML layout as @style/CodeFont, as illustrated in the TextView above.这个示例样式可以从 XML 布局引用为 @style/CodeFont,如上面的 TextView 所示。 A theme IS a style .一个主题就是一种风格 The only difference is a theme is a style applied to an entire Activity or app, rather than an individual View.唯一的区别是主题是应用于整个 Activity 或应用程序的样式,而不是单个视图。

When a style is applied as a theme, every view in the activity or app applies each style attribute that it supports.当样式作为主题应用时,活动或应用程序中的每个视图都会应用它支持的每个样式属性。 For example, if you apply the same CodeFont style as a theme for an activity, then all text inside that activity appears in a green monospace font.例如,如果您应用相同的 CodeFont 样式作为活动的主题,则该活动中的所有文本都以绿色等宽字体显示。

<resources>
    <style name="AppTheme" parent="Theme.Material">
        <item name="colorPrimary">#673AB7</item>
        <item name="colorPrimaryDark">#512DA8</item>
        <item name="colorAccent">#FF4081</item>
    </style>
</resources>

And the following example illustrates specifying values for the same attribute using references:以下示例说明使用引用为同一属性指定值:

<resources>
    <style name="AppTheme" parent="Theme.Material">
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <item name="colorAccent">@color/accent</item>
    </style>
</resources>

To set a theme for all the activities of your app, open the AndroidManifest.xml file and edit the tag to include the android:theme attribute with the style name.要为应用程序的所有活动设置主题,请打开 AndroidManifest.xml 文件并编辑标记以包含 android:theme 属性和样式名称。 For example:例如:

<application android:theme="@style/CustomTheme">

If you want a theme applied to just one activity in your app, then add the android:theme attribute to the tag instead.如果您希望将主题仅应用于应用程序中的一个活动,请改为将 android:theme 属性添加到标签中。

Just as Android provides other built-in resources, there are many pre- defined themes that you can use, to avoid writing them yourself.正如 Android 提供其他内置资源一样,您可以使用许多预定义的主题,以避免自己编写它们。 For example, you can use the Dialog theme and make your activity appear like a dialog box:例如,您可以使用 Dialog 主题并使您的活动看起来像一个对话框:

<activity android:theme="@android:style/Theme.Dialog">

Or if you want the background to be transparent, use the Translucent theme:或者,如果您希望背景透明,请使用半透明主题:

<activity android:theme="@android:style/Theme.Translucent">

You need to create your widget styles and add that in your app theme and finally add your app theme in manifest.您需要创建小部件样式并将其添加到您的应用主题中,最后在清单中添加您的应用主题。 No need to mention the style in every view.无需在每个视图中提及样式。 like this Inside values folder in the styles file:像这样在样式文件中的内部值文件夹:

   <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textViewStyle">@style/TextViewStyle</item>
   </style>

   <style name="TextViewStyle" parent="android:Widget.TextView">
    <item name="android:background">@color/TextView_background</item>
    <item name="android:textColor">#16F20A</item>
    <item name="android:textStyle">bold</item>
   </style>


In Your manifest file  inside application tag add the below line.
    <application
      android:theme="@style/AppTheme">
    </application>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM