2016-09-30 22 views
1

私は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に設定しました。あなたは、設定する必要があり、私があまりにもAndroidの開発にはかなり新しいですが、これはそれを修正するなら、私に知らせて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> 
+0

それはまさにそれでした。どうもありがとうございます! –

+0

@DarinBeaudreauお手伝いします。あなたは歓迎です –

+0

これは少しローカライズできますか? –

0

おそらくtext_container LinearLayoutの幅がmatch_parentであるためです。 wrap_conentに変更してみてください

+0

うん、そうだった。しかし、今はテキストの隣にホバーインしています。私はそれを右に固執するためにアンドロイド:layout_gravity = "右|中心"を使用しようとしましたが、それは助けにはなりません。 textContainerの重みを1に設定する必要がありますか? –

0

をlayout_weigh追加:あなたの第二の原因と

私はあなたがこの問題に実行されていると信じてこの場合、wrap_contentを使用する必要がある場合は、幅と高さがmatch_parentになります。あなたの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

私はアンドロイドのスタジオでレイアウトをプレビューすることができるように、私はアイコンとその一定のサイズを変更しました。 それは私の場合、私はRelativeLayoutに外のレイアウトを変更するだろうが、あなたがしたように行うこともできます。

android:layout_widthは、ウェイトを使用する要素の最終サイズではないことがわかります。ウェイト属性はビューを再構成します。 Spoiler:ネストされたレイアウトがたくさんある場合は高価な操作なので、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

関連する問題