Search code examples
javaandroidlistviewandroid-layoutbaseadapter

Android ListView row layout ignoring padding


I'm sure this is something simple that I am overlooking but I can't figure it out. I have a ListView using a custom adapter with a simple layout and my padding value seems to be ignored. Here's how I want the layout to be:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" style="@style/Widget.Layout.ComponentEntry" android:padding="@dimen/preferred_padding">
    <include layout="@layout/component_entry_summary" />
</LinearLayout>

But the padding is ignored. If I use a nested LinearLayout then it works but the inner LinearLayout seems pointless. This works:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" style="@style/Widget.Layout.ComponentEntry">
    <LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:padding="@dimen/preferred_padding">
        <include layout="@layout/component_entry_summary"  />
    </LinearLayout>
</LinearLayout>

The first layout works outside of a ListView so I'm stumped. For reference here is the style used (which also specifies a padding):

<style name="Widget">
    <item name="android:textAppearance">@style/TextAppearance</item>
</style>

<style name="Widget.Layout">
    <!-- Empty Style -->
</style>

<style name="Widget.Layout.ListViewItem">
    <item name="android:background">@drawable/listview_item_background</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:padding">@dimen/preferred_padding</item>
</style>

<style name="Widget.Layout.ComponentEntry" parent="Widget.Layout.ListViewItem">
    <item name="android:layout_height">@dimen/listview_item_height</item>
</style>

And the included layout:

<merge xmlns:android="http://schemas.android.com/apk/res/android" >
    <ImageView android:id="@+id/iconImage"
               android:layout_gravity="center_vertical"
               android:layout_height="@dimen/icon_size"
               android:layout_width="@dimen/icon_size"
               android:scaleType="fitCenter"
               android:src="@drawable/ic_launcher" />

    <LinearLayout android:layout_gravity="center_vertical"
                  android:layout_height="wrap_content"
                  android:layout_width="0dp"
                  android:layout_weight="1"
                  android:paddingLeft="@dimen/preferred_padding"
                  android:orientation="vertical">

        <TextView android:id="@+id/topText"
                  android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:paddingLeft="@dimen/preferred_padding"
                  android:singleLine="true"
                  android:text="@string/app_name"
                  android:textColor="@color/header_text_enabled"
                  android:textStyle="bold" />

        <TextView android:id="@+id/bottomText"
                  android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:ellipsize="marquee"
                  android:marqueeRepeatLimit="marquee_forever"
                  android:paddingLeft="@dimen/preferred_padding"
                  android:singleLine="true"
                  android:text="@string/app_name"
                  android:textColor="@color/header_text_enabled" />
    </LinearLayout>
</merge>

Here's how layout is inflated (I was originally supplying a null parent and thought that was the issue but it doesn't seem to be:

public View getView(int position, View view, ViewGroup parent) {
    ViewHolder viewHolder;

    if (view == null) {
        view = _layoutInflater.inflate(_layoutResourceId, parent, false);
        ...

Solution

  • Removing the background caused the padding on the outer LinearLayout to start working. Haven't dug into why yet but the problem is (partially) resolved at least.