如何在HorizontalScrollView中设置图片间距?
Hey there! Let's sort out that spacing issue between your sports icons. The reason layout_weight isn't helping here is because your inner LinearLayout has a wrap_content width, and you've already set fixed 85dp widths on your ImageViews—weight only works when the parent has a defined size (like match_parent) and you're trying to distribute remaining space.
Here are two simple, effective ways to add spacing between your images so the HorizontalScrollView's dark background shows through:
Method 1: Use layout_margin on each ImageView
This works for all Android versions, just add horizontal margins to each ImageView to create gaps between them. You can adjust the 8dp value to make the spacing bigger or smaller:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".TestOne" android:orientation="vertical"> <HorizontalScrollView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#393939"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/imgFudbal" android:layout_width="85dp" android:layout_height="85dp" android:background="#212222" android:src="@drawable/fudbal" android:onClick="showFudbal" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:layout_marginTop="4dp" android:layout_marginBottom="4dp" tools:ignore="OnClick"/> <ImageView android:id="@+id/imgBasketball" android:layout_width="85dp" android:layout_height="85dp" android:background="#212222" android:onClick="showBasketball" android:src="@drawable/basketball" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:layout_marginTop="4dp" android:layout_marginBottom="4dp" tools:ignore="OnClick"/> <ImageView android:id="@+id/imgTenis" android:layout_width="85dp" android:layout_height="85dp" android:background="#212222" android:clickable="true" android:onClick="showTenis" android:src="@drawable/tenis" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:layout_marginTop="4dp" android:layout_marginBottom="4dp" tools:ignore="OnClick"/> <ImageView android:id="@+id/imgRagbi" android:layout_width="85dp" android:layout_height="85dp" android:background="#212222" android:onClick="showRagbi" android:src="@drawable/ragbi" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:layout_marginTop="4dp" android:layout_marginBottom="4dp" tools:ignore="OnClick"/> <ImageView android:id="@+id/imgVaterpolo" android:layout_width="85dp" android:layout_height="85dp" android:background="#212222" android:onClick="showVaterpolo" android:src="@drawable/vaterpolo" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:layout_marginTop="4dp" android:layout_marginBottom="4dp" tools:ignore="OnClick"/> </LinearLayout> </HorizontalScrollView> <androidx.recyclerview.widget.RecyclerView android:id="@+id/testRecycler" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#393939"/> </LinearLayout>
I added small top/bottom margins too so the images don't touch the edges of the scroll view, but you can remove those if you don't need them.
Method 2: Use android:gap on the LinearLayout (API 31+)
If you're targeting Android 12 (API level 31) or higher, you can use the gap attribute on the inner LinearLayout to set uniform spacing between all child views in one line—no need to add margins to each ImageView:
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:gap="16dp" android:padding="4dp">
The padding here adds space between the scroll view edges and the first/last images, and gap handles the spacing between each icon.
Just remember to remove those layout_weight attributes from your ImageViews—they're not doing anything useful here and can cause unexpected behavior with fixed widths!
内容的提问来源于stack exchange,提问作者stefan josimovski

