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

Android LinearLayout, Can't get image to appear to right of a LinearLayout

我正在尝试让 "play" 图标的图像显示在我在 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>

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

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

可能是因为 text_container LinearLayout 的宽度为 match_parent。尝试将其更改为 wrap_conent

您已将 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 开发的新手,但请告诉我这是否解决了问题:

我相信您 运行 遇到了这个问题,因为您定义的第二个线性布局的宽度和高度为 match_parent,而在这种情况下您应该使用 wrap_content。因为您的 match_parent 正在使其大小与您之前的布局相同,即设备屏幕的大小,并将图像推到设备屏幕参数之外。

所以您的代码应该是:

<?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="wrap_content"
            android:layout_height="wrap_content"
            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 工作室中预览布局。 如果是我,我会将外部布局更改为 RelativeLayout,但也可以像您一样完成。

您会注意到 android:layout_width 不是使用 weights 的元素的最终大小,weights 属性将重新配置视图。剧透:如果有很多嵌套布局,这是一个昂贵的操作,因此我更喜欢 RelativeLayout。不过这个就够简单了。

您必须在父布局的子布局中使用 layout_weight 来分配可用的 space。

这里是:

<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="55dp"
        android:layout_height="55dp"
        android:src="@android:drawable/star_on"
        />

    <LinearLayout
        android:id="@+id/text_container"
        android:layout_width="10dp"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:paddingLeft="16dp"
        android:layout_weight="8">
        <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="55dp"
        android:layout_height="55dp"
        android:layout_gravity="center_vertical"
        android:paddingRight="8dp"
        android:src="@android:drawable/ic_media_play"
        android:layout_weight="1"/>
</LinearLayout>

希望对您有所帮助! =)