2016-09-30 150 views
1

我试图让一个“play”图标的图像出现在我在LinearLayout中的列表项的右侧。Android LinearLayout,无法让图像出现在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

我走在移动应用程序开发的一类了,所以我不是100%熟悉每一个布局呢。这是什么造成的?

回答

3

您已设置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> 
+0

就是这样。非常感谢! –

+0

@DarinBeaudreau我很乐意为您效劳。不客气 –

+0

您可以使这个少一点本地化? –

0

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

+0

是的,这是做到了。但现在它正在文本旁边徘徊。我试过使用android:layout_gravity =“right | center”来让它坚持正确的,但是这没有帮助。我应该将textContainer的权重设置为1吗? –

0

我非常新的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> 
+0

此解决方案将显示未在父代LinearLayout右侧对齐的图像。 –

0

我改变了图标和大小不变,所以我可以预览布局在我的Android工作室。 如果是我,我会将外部布局更改为RelativeLayout,但也可以像您那样完成。

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

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

这就是:

enter image description here

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

希望这有助于! =)

+0

@Darin Beaudreau你能解决你的问题吗?让我知道这是否有帮助=) – HenriqueMS