Custom item in topic

I want to create a style that I will use in android:textAppearancemy TextView. But this style is different at different API levels, so I want to have:

  • one common style (for all API levels),
  • some styles inherited from this common style in values-v16, values-v21 dirs,
  • one directive (style / attr?) I will put in the TextView parameter android:textAppearance(layout files are common for all API levels).

I tried several combinations from Google and Stack and finally ended up with something like this (which of course does not work):

values ​​/ attrs.xml

<attr name="myTextAppearance" format="reference" />

values ​​/ styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="myTextAppearance">@style/CommonTextAppearance</item>
</style>

<style name="CommonTextAppearance">
    <item name="android:textColor">#00f</item>
</style>

values-V16 / styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="myTextAppearance">@style/V16TextAppearance</item>
</style>

<style name="V16TextAppearance" parent="CommonTextAppearance">
    <item name="android:textSize">20sp</item>
</style>

Layout / activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@android:id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Lorem"
        android:textAppearance="?attr/myTextAppearance"/>

</RelativeLayout>

And currently I am getting an error:

Could not find the theme resource? attr / myTextAppearance for the current topic

, , . ?

+4

Source: https://habr.com/ru/post/1569893/


All Articles