Create a "theme" for a custom Android view containing multiple internal text views

I have a custom view "address_view.xml" that displays a person's name and street address, defined like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:orientation="vertical">

    <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:text="John Smith"/>

    <TextView
            android:id="@+id/street_address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="2"
            tools:text="1 Main St."/>
</LinearLayout>

      

This view is used on several pages in my application, but there are two options for font, text color and size for each of the text elements.

Is it possible to create "themes" just for this view that set the textFont / textColor of the name and address of the TextViews separately? For example, I would like to do something like:

<com.example.view.AddressView
    ...
    style="@style/Theme1" />

      

which sets the "name" of the TextView to use FontA, ColorA and Size1 and sets the "address" of the TextView to use FontB, ColorB and Size2.

This way I can use Theme1 on some pages and create another "Theme2" with a second font / color / size combination and use it on other pages.

+3


source to share


1 answer


First you need to define custom attributes and then use them in style. I'll use the triangle style as an example.

First determine what you want to change with your attributes and put them in /res/attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="Triangle">
        <attr name="triangleColor" format="color"/>
        <attr name="triangleStrokeColor" format="color"/>
        <attr name="triangleStrokeWidth" format="dimension"/>
    </declare-styleable>
</resources>

      

And in your custom view you need to read the values ​​you passed to

 // Get the values from XML
TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.Triangle, style, 0);

int tmp;

mTriangleColor = typedArray.getColor(R.styleable.Triangle_triangleColor, mTriangleColor);
mStrokeColor = typedArray.getColor(R.styleable.Triangle_triangleStrokeColor, mStrokeColor);

tmp = typedArray.getDimensionPixelSize(R.styleable.Triangle_triangleStrokeWidth, -1);
mStrokeWidth = tmp != -1 ? tmp : 2 * density; // Use 2dp as a default value

// Don't forget this!
typedArray.recycle();

      



And then define styles. NOTE. there are no xml namespaces for custom attribute elements, so no android:

<style name="defaultTriangle">
    <item name="triangleColor">#FF33B5E5</item>
    <item name="triangleStrokeColor">@android:color/black</item>
    <item name="triangleStrokeWidth">3dp</item>
</style>

      

And then just apply

<some.package.Triangle
    style="@style/defaultTriangle"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:padding="10dp"
    android:rotation="0"
    />

      

+1


source







All Articles