gpt4 book ai didi

Android LinearLayout,无法让图像显示在 LinearLayout 的右侧

转载 作者:行者123 更新时间:2023-11-29 17:13:53 24 4
gpt4 key购买 nike

我正在尝试让“播放”图标的图像显示在我在 LinearLayout 中的列表项的右侧。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dp">

<ImageView
android:id="@+id/album_image"
android:layout_width="@dimen/list_item_height"
android:layout_height="@dimen/list_item_height" />

<LinearLayout
android:id="@+id/text_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="@+id/artist_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Artist" />

<TextView
android:id="@+id/song_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Song Name" />
</LinearLayout>

<ImageView
android:id="@+id/play"
android:layout_width="@dimen/list_play_icon_height"
android:layout_height="@dimen/list_play_icon_height"
android:layout_gravity="center_vertical"
android:paddingRight="8dp"
android:src="@drawable/song_play" />
</LinearLayout>

但是,由于某种原因,图像出现在屏幕外(如下图所示)。

Android LinearLayout Problem

我现在正在上移动应用程序开发类(class),所以我还不是 100% 熟悉每种布局。这是什么原因造成的?

最佳答案

您已将 text_container 的宽度设置为 match_parent。您应该设置为 0dp 并为此元素添加 layout_weigh

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dp">

<ImageView
android:id="@+id/album_image"
android:layout_width="@dimen/list_item_height"
android:layout_height="@dimen/list_item_height" />

<LinearLayout
android:id="@+id/text_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="@+id/artist_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Artist" />

<TextView
android:id="@+id/song_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Song Name" />
</LinearLayout>

<ImageView
android:id="@+id/play"
android:layout_width="@dimen/list_play_icon_height"
android:layout_height="@dimen/list_play_icon_height"
android:layout_gravity="center_vertical"
android:paddingRight="8dp"
android:src="@drawable/song_play" />
</LinearLayout>

关于Android LinearLayout,无法让图像显示在 LinearLayout 的右侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39796126/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com