I want to create a composite element and have different styles for it, but I donโt see how to achieve it so far with the help of one style, I have to use different styles.
To illustrate my problem, I will describe an example scenario. Imagine that I have a list with items, these items are users, and I have an avatar, username and email address. I want the style of each of these 3 different options to be dedicated to my topic.
What I'm trying to achieve:
(file res / layout / item_user.xml, this will be overpriced for ListView)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
style="@style/CustomStyleA">
<ImageView
style="avatarStyle"
android:id="@+id/avatar"
android:src="@drawable/avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
style="usernameStyle"
android:id="@+id/username"
android:text="Username"
android:layout_toRightOf="@id/avatar"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
style="emailStyle"
android:id="@+id/email"
android:text="fakemail@mail.com"
android:layout_toRightOf="@id/avatar"
android:layout_below="@id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
(file res / values โโ/styles.xml)
<resources>
<style name="CustomStyleA">
<item name="avatarStyle">@style/CustomStyleA.Avatar</item>
<item name="usernameStyle">@style/CustomStyleA.Username</item>
<item name="emailStyle">@style/CustomStyleA.Email</item>
</style>
<style name="CustomStyleA.Avatar">
</style>
<style name="CustomStyleA.Username" parent="android:TextAppearance">
</style>
<style name="CustomStyleA.Email" parent="android:TextAppearance">
</style>
<style name="CustomStyleB">
<item name="avatarStyle">@style/CustomStyleB.Avatar</item>
<item name="usernameStyle">@style/CustomStyleB.Username</item>
<item name="emailStyle">@style/CustomStyleB.Email</item>
</style>
<style name="CustomStyleB.Avatar">
</style>
<style name="CustomStyleB.Username" parent="android:TextAppearance">
</style>
<style name="CustomStyleB.Email" parent="android:TextAppearance">
</style>
</resources>
What i really need to do
(file res / layout / item_user.xml, this will be overpriced for ListView)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content">
<ImageView
style="@style/CustomStyleA.Avatar"
android:id="@+id/avatar"
android:src="@drawable/avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
style="@style/CustomStyleA.Email"
android:id="@+id/username"
android:text="Username"
android:layout_toRightOf="@id/avatar"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
style="@style/CustomStyleA.Email"
android:id="@+id/email"
android:text="fakemail@mail.com"
android:layout_toRightOf="@id/avatar"
android:layout_below="@id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
(file res / values โโ/styles.xml)
<resources>
<style name="CustomStyleA.Avatar">
</style>
<style name="CustomStyleA.Username" parent="android:TextAppearance">
</style>
<style name="CustomStyleA.Email" parent="android:TextAppearance">
</style>
<style name="CustomStyleB.Avatar">
</style>
<style name="CustomStyleB.Username" parent="android:TextAppearance">
</style>
<style name="CustomStyleB.Email" parent="android:TextAppearance">
</style>
</resources>
, , , . .