본문 바로가기

Android

[Android] Style your TextView

* Style your TextView - Extract the style

- A style is a collection of attributes that specify the appearance and format for a view. A style can include font color, font size, background color, padding, margin, and other common attributes.

 

- You can extract(추출) the name text view's formatting into a style and reuse the style for any number of views in your app. Reusing a style gives your app a consistent(일관된) look when you have multiple views. Using styles also allows you to keep these common attributes in one location.

 

  1. Right-click the TextView in the Component Tree and select Refactor > Extract Style.
  2. In the Extract Android Style dialog, clear the layout_width checkbox, the layout_height checkbox, and the textAlignment checkbox. These attributes are usually different for each view, so you don't want them to be part of the style.
  3. In the Style name field, enter NameStyle.
  4. Click OK.

  5. A style is also a resource, so the style is saved in the res/values/ folder in a styles.xml file. Open styles.xml and examine the generated code for the NameStyle style, which will look similar to this:
<style name="NameStyle">
   <item name="android:layout_marginTop">@dimen/layout_margin</item>
   <item name="android:fontFamily">@font/roboto</item>
   <item name="android:paddingTop">@dimen/small_padding</item>
   <item name="android:textColor">@android:color/black</item>
   <item name="android:textSize">@dimen/text_size</item>
</style>

 

 

 6. Open activity_main.xml and switch to the Text tab. Notice that the generated style is being used in the text view as style="@style/NameStyle"

 

출처: https://codelabs.developers.google.com/codelabs/kotlin-android-training-linear-layout/index.html#5

 

Android Kotlin Fundamentals 02.1: Linear layout using the Layout Editor

In the AboutMe app, you can showcase interesting facts about yourself, or you can customize the app for a friend, family member, or pet. This app displays a name, a DONE button, a star image, and some scrollable text.

codelabs.developers.google.com