title: Using <include> in android.support.v7.widget.GridLayout not displaying correctly
body: <p>I'm trying to use <code>&lt;include&gt;</code> to include <code>ImageButton</code>s inside a <code>android.support.v7.widget.GridLayout</code>. The problem is, if I don't specify all the attributes explicitly on my <code>&lt;include&gt;</code> elements, they don't display properly. In other words, the include is pointless since I have to redeclare all the attributes on every <code>&lt;include&gt;</code> element.</p>
  
  <p><strong>dialpad_button.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;ImageButton
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:grid="http://schemas.android.com/apk/res-auto"
      android:layout_width="0dp"
      android:layout_height="0dp"
      grid:layout_columnWeight="1"
      grid:layout_rowWeight="1"
      grid:layout_gravity="fill"
      android:layout_margin="5dp"
      android:scaleType="centerInside"/&gt;
  </code></pre>
  
  <p><strong>dialpad_view.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.v7.widget.GridLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:grid="http://schemas.android.com/apk/res-auto"
      android:id="@+id/dialpad_grid_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_centerHorizontal="true"
      grid:alignmentMode="alignBounds"
      grid:columnCount="2"
      grid:rowCount="1"&gt;
  
      &lt;include
          android:id="@+id/dialpad_view_btn1"
          android:src="@drawable/dialpad_1"
          layout="@layout/dialpad_button" /&gt;
  
      &lt;include
          android:id="@+id/dialpad_view_btn2"
          android:src="@drawable/dialpad_2"
          layout="@layout/dialpad_button" /&gt;
  
  
  &lt;/android.support.v7.widget.GridLayout&gt;
  </code></pre>
  
  <p>If I declare all the attributes directly on the <code>&lt;include&gt;</code>s, here's what it looks like in XML:</p>
  
  <pre><code>    &lt;include
          layout="@layout/dialpad_button"
          android:id="@+id/dialpad_view_btn1"
          android:src="@drawable/dialpad_1"
          android:layout_width="0dp"
          android:layout_height="0dp"
          grid:layout_columnWeight="1"
          grid:layout_rowWeight="1"
          grid:layout_gravity="fill"
          android:layout_margin="5dp"
          android:scaleType="centerInside" /&gt;
  </code></pre>
  
  <p>As you can see in the comparison image below, the buttons now scale properly but the images are nowhere to be seen.</p>
  
  <p>And if I change the <code>&lt;include&gt;</code>s to <code>ImageButton</code>, things work as expected.</p>
  
  <pre><code>    &lt;ImageButton
          android:id="@+id/dialpad_view_btn1"
          android:src="@drawable/dialpad_1"
          android:layout_width="0dp"
          android:layout_height="0dp"
          grid:layout_columnWeight="1"
          grid:layout_rowWeight="1"
          grid:layout_gravity="fill"
          android:layout_margin="5dp"
          android:scaleType="centerInside" /&gt;
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/BkqYt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BkqYt.png" alt="Comparison of different XML-layouts"></a></p>
  
  <p>Is there a workaround to this? </p>
  
  <p>Since I'm going to have 12 near-identical buttons in my dialpad, I'd really like to clean the XML up by including a "template" and only modifying the necessary attributes for each button (i.e. <code>src</code> and <code>id</code>).</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Trying to use styles, as suggested in one answer, did not work. None of the views that I applied the styles to get displayed. What's even more strange, even if I just apply the style to one of the views in the <code>GridLayout</code>, only the last view gets displayed (I've shortened down the sample code here to only two views for readability, in reality I have twelve). </p>
  
  <p>Here's the style I tried using:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:grid="http://schemas.android.com/apk/res-auto" &gt;
  
      &lt;style name="dialPadButtonStyle"&gt;
          &lt;item name="android:layout_width"&gt;0dp&lt;/item&gt;
          &lt;item name="android:layout_height"&gt;0dp&lt;/item&gt;
          &lt;item name="grid:layout_columnWeight"&gt;1&lt;/item&gt;
          &lt;item name="grid:layout_rowWeight"&gt;1&lt;/item&gt;
          &lt;item name="grid:layout_gravity"&gt;fill&lt;/item&gt;
          &lt;item name="android:layout_margin"&gt;5dp&lt;/item&gt;
          &lt;item name="android:scaleType"&gt;centerInside&lt;/item&gt;
          &lt;item name="android:background"&gt;@android:color/transparent&lt;/item&gt;
      &lt;/style&gt;
  &lt;/resources&gt;
  </code></pre>