Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
        ...
like image 385
Cory Charlton Avatar asked Feb 20 '23 15:02

Cory Charlton


2 Answers

I ran into the same with issue,when I removed the code to set the background the padding worked.

Edit: Found out what is causing it, it is the padding line in the 9 patch images, I thought if you left it out of the drawing it would use the padding in the layout xml. But it actually then uses the stretchable area also as the drawable area.

If the padding lines are not included, Android uses the left and top lines to define this drawable area, effectively defining the padding.

http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch

like image 175
pt123 Avatar answered Feb 23 '23 05:02

pt123


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.

like image 21
Cory Charlton Avatar answered Feb 23 '23 06:02

Cory Charlton