patritri
patritri

Reputation: 13

Inflate layout with defined style

I'm implementing and app with a custom list adapter. When I inflate each list item which have a defined style the style doesn't seem to work. I call the inflate doing this:

convertView = inflater.inflate(R.layout.widget,null); 

The layout is this one:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="?widgetWrapperStyle">

    <LinearLayout
      style="@style/stat" />

    <RelativeLayout
      style="?widgetStyle"
      android:layout_below="@+id/widgetStat" >

        <TextView
          android:id="@+id/head"/>

        <LinearLayout
          android:id="@+id/body" />
</RelativeLayout>

My attrs.xml and style.xml are:

<?xml version="1.0" encoding="utf-8"?>
<resources>

  <declare-styleable name="widgetWrapper">
        <attr name="widgetWrapperStyle" format="reference" />
    </declare-styleable>
    <declare-styleable name="widget">
        <attr name="widgetStyle" format="reference" />
    </declare-styleable>

</resources>

<style name="AppThemeDark" parent="AppBaseThemeDark">
    <item name="widgetWrapperStyle">@style/widgetWrapperDark</item>
    <item name="widgetStyle">@style/widget</item>
</style>

<style name="widgetWrapperDark" parent="widgetWrapper">
    <item name="android:background">@color/list_item_background_dark</item>
</style>

<style name="widget">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:padding">@dimen/widget_padding</item>
</style>

Non of the 'style="?xxxx"' seems to be working. When the view gets inflate the background color is not the correct one.

Upvotes: 1

Views: 3803

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007584

When inflating layouts, it is important to use a properly-configured LayoutInflater. In particular, it needs to be created from an Activity, ideally by just calling getLayoutInflater() (or getSupportLayoutInflater() for a SherlockActivity and kin, IIRC).

Dave Smith has an excellent blog post reviewing the various types of Context, including covering the issues regarding themes and layout inflation.

Upvotes: 4

Related Questions