How to set a theme to a customView?

Viewed 718

I have this style.xml:

<style name="header_has_selected_account" parent="TextAppearance.AppCompat">
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_width">wrap_content</item>

<item name="android:layout_marginStart">
  @dimen/account_menu_account_list_item_avatar_margin_end
</item>
<item name="android:layout_marginEnd">
  @dimen/account_menu_account_list_item_margin_end
</item>
<item name="android:layout_marginLeft">
  @dimen/account_menu_account_list_item_avatar_margin_end
</item>
<item name="android:layout_marginRight">
  @dimen/account_menu_account_list_item_margin_end
</item>
  </style>

I have a CustomView with a layout with two TextViews and an ImageView.

How can I set the style to only one of the TextViews (not custom TextView)? Do I have to create 2 CustomView and a custom ImageView as I have to pass the style throw their ctor?

1 Answers

You don't need to create new custom views. If you have the reference to your TextView all you need to use is the TextViewCompat to apply a certain style resource.

Like this:

TextViewCompat.setTextAppearance(yourTextView, yourStyleId);

Note: TextViewCompat is available on android.support.v4.


From the documentation:

Sets the text appearance from the specified style resource.

Related