Как разделить изображения в горизонтальной компоновке / scrollview?

#android-studio #android-layout #android-linearlayout #android-imageview #android-scrollview

#android-studio #android-layout #android-linearlayout #android-imageview #android-scrollview

Вопрос:

Я создал этот дизайн пользовательского интерфейса с помощью horizontal ScrollView, в котором есть изображения спортивных значков, но они расположены близко друг к другу, я хочу, чтобы они были разделены, чтобы я мог видеть фон ScrollView между ними…

Я пытался добавить layout_weight к изображениям, но, к сожалению, это не тот случай.

Изображение, как оно должно выглядеть и как оно выглядит сейчас..

Это мой XML-код

 <?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"
                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"
                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"
                tools:ignore="OnClick" />

            <ImageView
                android:id="@ id/imgRagbi"
                android:layout_width="85dp"
                android:layout_height="85dp"
                android:layout_weight="1"

                android:background="#212222"
                android:onClick="showRagbi"
                android:src="@drawable/ragbi"
                tools:ignore="OnClick" />

            <ImageView
                android:id="@ id/imgVaterpolo"
                android:layout_width="85dp"
                android:layout_height="85dp"
                android:layout_weight="1"

                android:background="#212222"
                android:onClick="showVaterpolo"
                android:src="@drawable/vaterpolo"
                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>

  

Комментарии:

1. указав marginStart и marginEnd для ваших просмотров изображений

2. @AbdurRehman Большое вам спасибо, я действительно ценю ваш ответ!

Ответ №1:

попробуйте этот код :

 <?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"
    android:orientation="vertical">

    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#393939"
        android:paddingTop="10dp">

        <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:layout_marginLeft="5dp"
                android:layout_marginRight="10dp"
                android:background="#212222"
                android:onClick="showFudbal"
                android:src="@drawable/img_natural"
                tools:ignore="OnClick" />

            <ImageView
                android:id="@ id/imgBasketball"
                android:layout_width="85dp"
                android:layout_height="85dp"
                android:layout_marginRight="10dp"
                android:background="#212222"
                android:onClick="showBasketball"
                android:src="@drawable/img_natural"
                tools:ignore="OnClick" />

            <ImageView
                android:id="@ id/imgTenis"
                android:layout_width="85dp"
                android:layout_height="85dp"
                android:layout_marginRight="10dp"
                android:background="#212222"
                android:clickable="true"
                android:onClick="showTenis"
                android:src="@drawable/img_natural"
                tools:ignore="OnClick" />

            <ImageView
                android:id="@ id/imgRagbi"
                android:layout_width="85dp"
                android:layout_height="85dp"
                android:layout_marginRight="10dp"
                android:layout_weight="1"
                android:background="#212222"
                android:onClick="showRagbi"
                android:src="@drawable/img_natural"
                tools:ignore="OnClick" />

            <ImageView
                android:id="@ id/imgVaterpolo"
                android:layout_width="85dp"
                android:layout_height="85dp"
                android:layout_marginRight="10dp"
                android:layout_weight="1"
                android:background="#212222"
                android:onClick="showVaterpolo"
                android:src="@drawable/img_natural"
                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>