从appcompat到将深色主题应用于android的材料组件2

    科技2022-08-01  117

    Using the Material Design Typography system in your Android apps

    在Android应用中使用Material Design Typography系统

    Hey readers,

    读者们,

    This is the 2nd part of how to move from AppCompat to the Material Theming Components. Check out the 1st part here.

    这是如何从AppCompat迁移到Material Theming Components的第二部分。 在这里查看第一部分。

    Till now we have implemented the color design system into our apps and taken the first step to implement the new Material Design library. The next step is to implement a consistent Typography(fonts/text size) in our apps.

    到目前为止,我们已经将色彩设计系统实现到了我们的应用程序中,并迈出了实现新的Material Design库的第一步。 下一步是在我们的应用程序中实现一致的版式(字体/文本大小)。

    版式 (Typography)

    Typography is like the fonts and font sizes applied to your views(Text Views, Buttons, etc.). You would want to achieve a common font either to all of the components or only a few of them. Material Design library helps us to achieve this easily. In the future, if anything changes, we can change the things in one place and it will be reflected throughout our project.

    排版就像应用于视图(文本视图,按钮等)的字体和字体大小一样。 您可能想要为所有组件或仅其中几个组件实现通用字体。 材料设计库可帮助我们轻松实现这一目标。 将来,如果发生任何更改,我们可以在一处更改所有内容,并将在整个项目中反映出来。

    The Material Design type scale provides 13 typography styles for everything from headlines to body text and captions. Each style has a clear meaning and intended application within an interface.

    “材料设计” 类型比例为标题,正文和标题提供了13种字体样式。 每种样式在界面内都有明确的含义和预期的应用程序。

    Important attributes, such as the typeface, font-weight, and letter case, can be modified to match your brand and design.

    可以修改重要属性,例如字体,字体粗细和字母大小写,以匹配您的品牌和设计。

    Steps to use the Typography system:

    使用版式系统的步骤 :

    Create a new type.xml file under res folder i.e. res/values/type.xml — Though we can put our text styles in the styles.xml file, it is better to create a new file especially for the text appearance. It improves readability and separation of concerns. Therefore, anything related to text appearances will move into the type.xml file. Copy all the text appearances related styles into type.xml file.

    在res文件夹下创建一个新的type.xml文件,即res / values / type.xml —尽管我们可以将文本样式放入styles.xml文件中,但是最好创建一个新文件,尤其是针对文本外观。 它提高了可读性和关​​注点分离。 因此,与文本外观有关的所有内容都将移入type.xml文件。 将所有与文本外观相关的样式复制到type.xml文件中。

    Remove/replace some attributes — With the new material design system, there are certain attributes that are mapped 1 to 1 with the old material design attributes. So some attributes can be used as it is. For others, you should replace them with their MDC counterparts. Check the below list to find out these attributes:

    删除/替换某些属性 -使用新的材料设计系统,某些属性与旧的材料设计属性按1到1映射。 因此,可以按原样使用某些属性。 对于其他人,应将其替换为MDC对应的人。 检查以下列表以找出这些属性:

    This means that:

    这意味着:

    textAppeanceHeadline4 = TextAppearance.AppCompat.Display1

    textAppeanceHeadline4 = TextAppearance.AppCompat.Display1

    android:textAppearance="@style/TextAppearance.AppCompat.Display1"android:textAppearance="?attr/textAppearanceHeadline4"// are equal so changing them is optional

    3. Override the default text appearances to implement your custom fonts and styles — You can create new text styles which override the default text styles. Here’s an example and its application:

    3.覆盖默认文本外观以实现您的自定义字体和样式 -您可以创建覆盖默认文本样式的新文本样式。 这是一个示例及其应用:

    <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="My text view" android:layout_margin="8dp" android:textAlignment="center" android:textAppearance="?attr/textAppearanceHeadline4" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> A custom-styled text view ` 自定义样式的文本视图

    4. Use consistent naming (optional but recommended)— Please notice how I name the text appearance. This resolves the ambiguity between the library text appearances and your app’s text appearances. It also helps to make the code review easier where a theme might be used instead of a widget style. The naming convention is:

    4. 使用一致的命名(可选,但建议使用) —请注意我如何命名文本外观。 这解决了库文本外观与应用程序的文本外观之间的歧义。 它还可以使使用主题而非窗口小部件样式的地方的代码审查更加容易。 命名约定为:

    Naming convention: StyleType.AppName.SubGroupName.Variant

    命名约定:StyleType.AppName.SubGroupName.Variant

    Eg1: TextAppearance.MyAppName.Headline4Eg2: Widget.MyAppName.Toolbar.BlueEg3: ShapeAppearance.MyModuleName.Card

    Note: Each textAppearance style defines a set of attributes such as: android:fontFamily, android:textStyle, android:letterSpacing, android:textAllCaps, fontFamily which you can customize. Trying to change textColor i.e. android:textColor=”?attr/colorPrimary” or changing android:ellipsize on a TextApperance style won’t work.

    注意 :每种textAppearance样式都定义了一组属性,您可以自定义这些属性,例如android:fontFamily, android:textStyle, android:letterSpacing, android:textAllCaps, fontFamily 。 尝试更改textColor即android:textColor=”?attr/colorPrimary”或更改TextApperance样式上的android:ellipsize均无效。

    With this knowledge, you can extend the text appearances to follow your brand style. In the next post, we will see how to apply the Shape design system to our views and change the corners of our views.

    有了这些知识,您就可以扩展文字外观以跟随您的品牌风格。 在下一篇文章中,我们将看到如何将Shape设计系统应用于我们的视图并改变视图的角落。

    翻译自: https://medium.com/@shubham08gupta/from-appcompat-to-material-components-applying-dark-theme-to-android-part-2-18839b6a3f0f

    相关资源:微信小程序源码-合集6.rar
    Processed: 0.012, SQL: 8