Android: как получить 3 элемента рядом - PullRequest
0 голосов
/ 18 октября 2019

Я пытаюсь иметь 3 элемента рядом, как это:

enter image description here

Второе изображение никогда не отображается. Где моя ошибка?

        <ImageView
                android:id="@+id/asset_cat_image"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginStart="10dp"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginBottom="16dp"
                android:src="@mipmap/ic_launcher" />

        <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/asset_cat_image"
                android:layout_centerVertical="true"
                android:layout_toEndOf="@+id/asset_cat_image">

            <TextView
                    android:id="@+id/asset_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="16sp"
                    android:textStyle="bold"
                    android:maxLines="1"
                    android:textColor="@color/cardview_head"
                    android:ellipsize="end"
                    tools:text="Main Text"
                    android:layout_marginRight="10dp"
                    android:layout_marginEnd="10dp" />

            <TextView
                    android:id="@+id/asset_category"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textStyle="italic"
                    android:textSize="14sp"
                    android:textColor="@color/cardview_subhead"
                    tools:text="Sub Text"
                    android:maxLines="1"
                    android:ellipsize="end"
                    android:layout_marginRight="10dp"
                    android:layout_marginEnd="10dp" />

        </LinearLayout>

        <ImageView
                android:id="@+id/collapseimg"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginStart="10dp"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginBottom="16dp"
                android:src="@mipmap/ic_launcher" />

    </RelativeLayout>

1 Ответ

1 голос
/ 18 октября 2019

Хотя ширина LinearLayout s равна match_parent, по умолчанию она смещает каждый вид после него. Чтобы этого избежать, нужно сказать, какой вид следует разместить после него. В вашем случае это должно быть:

<LinearLayout
    ...
    android:layout_toRightOf="@+id/asset_cat_image"
    android:layout_toLeftOf="@+id/collapseimg">

Затем выровняйте ваше второе изображение по правой стороне:

<ImageView
    ...
    android:layout_alignParentRight="true"/>
...