0

I have a horizontal LinearLayout nested in a vertical LinearLayout nested in a RelativeLayout. It has 5 imageviews which fill the width and I'm using weight to space evenly. I randomly fill between 1 and 5 of the imageviews at runtime. They remain spaced evenly but instead of hugging the left side of the parent, which I read in the docs is the default, they hug the right. I've tried gravity:left and scaleType="fitStart". I've tried nesting this layout in another relative layout and using alignParentLeft. Using a RelativeLayout instead of the LinearLayout will align the images to the left but doesn't space them evenly so isn't an option.

Below are the relevant pieces of my XML file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Game1" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="7">

<LinearLayout
    android:id="@+id/animal_images_row1"
    android:layout_width="fill_parent"
    android:layout_weight="1"
    android:layout_height="0dip"
    android:layout_marginTop="5dip"
    android:layout_marginBottom="5dip"
    android:layout_marginLeft="5dip"
    android:layout_marginRight="5dip"
    >
<ImageView 
    android:layout_height="wrap_content"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:id="@+id/block1"
    android:contentDescription="@string/ph"
    android:scaleType="centerInside"

    />
<ImageView 
    android:layout_height="wrap_content"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:id="@+id/block2"
    android:contentDescription="@string/ph"
    android:scaleType="centerInside" />
<ImageView 
    android:layout_height="wrap_content"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:id="@+id/block3"
    android:contentDescription="@string/ph" 
    android:scaleType="centerInside"/>
<ImageView 
    android:layout_height="wrap_content"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:id="@+id/block4"
    android:contentDescription="@string/ph"
    android:scaleType="centerInside" />
<ImageView  
    android:layout_height="wrap_content"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:id="@+id/block5"
    android:contentDescription="@string/ph"
    android:scaleType="centerInside" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>

I removed the rest of the xml file for brevity. Thanks in advance for the help!

EDIT: Add screen shots. 2 of 5 ImageViews are occupied. Aligning to right when I want them aligned to left.

All 5 ImageViews occupied and correctly aligned.

Casey Murray
  • 1,582
  • 17
  • 21
  • Can you please post the desired sample and the one you have now. Sorry but, I can't quite get you. – Milan Aug 10 '13 at 02:49
  • Sure! I am thinking a screen shot will help. Give me a few minutes to get one posted. – Casey Murray Aug 10 '13 at 03:00
  • Still figuring out how the image descriptions work. I added them but they aren't showing up. The first is 2 of 5 ImageViews occupied and aligning to the right when I want them aligned to the left. The second is all 5 ImageViews occupied and properly aligned. – Casey Murray Aug 10 '13 at 03:48
  • Milanix - thanks for the message. I was talking about the image description on here, though =) – Casey Murray Aug 10 '13 at 03:55
  • "android:contentDescription" isn't showed on the ImageView, it is used only for accessibility purpose. If you want to show text and image together, the easiest option is to use TextView with "android:drawable" – Milan Aug 10 '13 at 03:57
  • You can use `RelativeLayout` and then use the properties to properly left / right align your views :) – An SO User Aug 13 '13 at 02:32

1 Answers1

0

Thanks for the comments! Every little insight helps =) I couldn't figure out why they were sticking to the right and nothing I would do could force them to the left and it turns out that in my code I had

imgview.bringToFront();

as a temporary fix while I was figuring the layout out and after removing that line they stuck to the left properly.

Casey Murray
  • 1,582
  • 17
  • 21