title: Obtain styled attributes for child from parent's style definition
body: <p>The question title is probably nonsensical. I am creating a bunch of custom views that will be placed in a single parent layout - a custom <code>FrameLayout</code>. </p>
  
  <p>These custom views have their own style attr which are set using the parent's style attr. </p>
  
  <p>As an example, consider <code>Parent</code> to be the custom <code>FrameLayout</code>. Its style <code>attr</code> is defined in <code>attrs.xml</code>:</p>
  
  <pre><code>&lt;attr name="parentStyleAttr" format="reference" /&gt;
  </code></pre>
  
  <p>The <code>Child</code> also has its attr:</p>
  
  <pre><code>&lt;attr name="childStyleAttr" format="reference" /&gt;
  </code></pre>
  
  <p>And <code>Parent</code> defines its styleable attr as:</p>
  
  <pre><code>&lt;declare-styleable name="Parent"&gt;
      &lt;attr name="childStyleAttr" /&gt;
  &lt;/declare-styleable&gt;
  </code></pre>
  
  <p><code>Child's</code> styleable attr:</p>
  
  <pre><code>&lt;declare-styleable name="Child"&gt;
      &lt;attr name="childBgColor" format="color" /&gt;
  &lt;/declare-styleable&gt;
  </code></pre>
  
  <p>Following this, I define a style for the parent:</p>
  
  <pre><code>&lt;style name="ParentStyle"&gt;
      &lt;item name="childStyleAttr"&gt;@style/ChildStyle&lt;/item&gt;
  &lt;/style&gt;
  </code></pre>
  
  <p>and one for <code>Child</code>:</p>
  
  <pre><code>&lt;style name="ChildStyle"&gt;
      &lt;item name="childBgColor"&gt;@color/blah&lt;/item&gt;
  &lt;/style&gt;
  </code></pre>
  
  <p>For <code>Parent</code>, I set up <code>parentStyleAttr</code> in the app's theme:</p>
  
  <pre><code>&lt;!-- Base application theme. --&gt;
  &lt;style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"&gt;
      &lt;item name="parentStyleAttr"&gt;@style/ParentStyle&lt;/item&gt;
  &lt;/style&gt;
  </code></pre>
  
  <p>Now, when <code>Parent</code> is created, it inflates a layout containing <code>Child</code>:</p>
  
  <pre><code>LayoutInflater.from(getContext()).inflate(R.layout.child, this, true);
  </code></pre>
  
  <p>During <code>Child's</code> initialization, I need to read the value of the style attribute set in <code>@style/ChildStyle</code> - <code>childBgColor</code>.</p>
  
  <p>This doesn't work:</p>
  
  <pre><code>final TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.Child, R.attr.childStyleAttr, R.style.ChildStyle);
  </code></pre>
  
  <p>The way I am currently reading <code>attr/childBgColor</code> is:</p>
  
  <pre><code>public Child(Context context, AttributeSet attrs, int defStyleAttr) {
      super(createThemeWrapper(context), attrs, defStyleAttr);
      initialize(attrs, defStyleAttr, R.style.ChildStyle);
  }
  
  private static ContextThemeWrapper createThemeWrapper(Context context) {
      final TypedArray forParent = context.obtainStyledAttributes(
              new int[]{ R.attr.parentStyleAttr });
      int parentStyle = forParent.getResourceId(0, R.style.ParentStyle);
      forParent.recycle();
  
      TypedArray forChild = context.obtainStyledAttributes(parentStyle,
              new int[]{ R.attr.childStyleAttr });
      int childStyleId = forChild.getResourceId(0, R.style.ChildStyle);
      forChild.recycle();
  
      return new ContextThemeWrapper(context, childStyleId);
  }
  
  void initialize(AttributeSet attrs, int defStyleAttr, int defStyleRes) {
      Context context = getContext();
      final Resources res = getResources();
  
      final TypedArray a = context.obtainStyledAttributes(R.styleable.Child);
  
      ....
  }
  </code></pre>
  
  <p>I am not confident if this is the right approach. Can someone help shed some some light on this?</p>