Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android LinearLayout,Can';无法使图像显示在线性布局的右侧_Android_Android Linearlayout_Android View - Fatal编程技术网

Android LinearLayout,Can';无法使图像显示在线性布局的右侧

Android LinearLayout,Can';无法使图像显示在线性布局的右侧,android,android-linearlayout,android-view,Android,Android Linearlayout,Android View,我试图在线性布局中的列表项右侧显示一个“播放”图标的图像 <?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" andro

我试图在线性布局中的列表项右侧显示一个“播放”图标的图像

<?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>

但是,由于某些原因,图像显示在屏幕外(如下图所示)


我现在正在上一门关于移动应用程序开发的课程,所以我还不是100%熟悉每个布局。原因是什么?

可能是因为
文本\u容器
线性布局的宽度与父项匹配。尝试将其更改为“换行内容”

您已将文本容器的宽度设置为“匹配父对象”。您应该设置为0dp,并为此元素添加布局重量

<?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\u parent,而在这种情况下,您应该使用wrap\u内容。因为您的match_父级正在使其与之前的布局大小相同,即设备屏幕的大小,并将图像推到设备屏幕参数之外

因此,您的代码应该是:

<?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 studio中预览布局。 如果是我,我会将外部布局更改为RelativeLayout,但也可以像您一样完成

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

您必须在父布局的子级中使用
layout\u weight
,以分配可用空间

这是:



希望这有帮助!=)

是的,就是这样。但现在它在文本旁边徘徊。我曾尝试使用安卓:layout_gravity=“right | center”让它保持在右侧,但这没有帮助。我应该将textContainer的重量设置为1吗?就是这样。非常感谢你@我很高兴能帮助你。很好,你能不能把它本地化一点?这个解决方案会在父线性布局的右边显示不对齐的图像。@Darin Beaudreau你能解决你的问题吗?让我知道这是否有帮助=)
<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>