0

This piece of XML will correctly display a button which covers the entire row then 2 buttons which are weighted evenly and share 50% of the space like so:

__________________________
|________________________|
||       ButtonA        ||
||______________________||
|____________ ___________|
|| ButtonB  | | ButtonC ||
||__________| |_________||
|________________________|

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_height="fill_parent" android:layout_width="fill_parent">
<TableRow android:layout_height="fill_parent" android:layout_width="fill_parent"
 android:layout_weight="1">
    <Button android:id="@+id/b_a"
        android:layout_width="wrap_content" android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="Button A" />
</TableRow>
<TableRow android:layout_height="fill_parent" android:layout_width="fill_parent"
 android:layout_weight="1">
    <Button android:id="@+id/b_b"
        android:layout_width="wrap_content" android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="Button B" />
    <Button android:id="@+id/b_c"
        android:layout_width="wrap_content" android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="Button C" />
</TableRow>

I want to replace Button A with an ImageView which will stretch across just like Button A is currently. However, when you replace ButtonA with:

<ImageView android:id="@+id/img_a"
        android:layout_width="wrap_content" android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src=@drawable/imagename />

This affects the weighting of Buttons B and C, making button B cover most of the screen and making C really squashed up. It appears to be related to the dimensions of the image (I used 320x50).

__________________________
|________________________|
||       Image A        ||
||______________________||
|___________________ ____|
|| ButtonB          ||bC||
||__________________||__||
|________________________|

How can I insert this ImageView without affecting the rest of the table rows?

Dororo
  • 3,420
  • 2
  • 30
  • 46

1 Answers1

0

I think you are being a little too liberal with your use of layout_weight. this is more an option used for having multiple items within a layout that need to be sized dynamically according to screen size (just like you have with buttons B and C). I would get rid of the rest of your layout_weights as they essentially arent doing anything and may have some underlying funky behavior. Also, layout_weight is a ratio of item-size to screen that ranges from 0-1. Both buttons B and C want to have 100% of the horizontal space allocated for them. Thus, I believe your issue is that button B is being prioritized higher than button C. try changing your layout_weight's for those 2 items to 0.5, This way they each desire 50% of the space allocated. Let me know how this works for you!

Adam Storm
  • 724
  • 1
  • 6
  • 13
  • No good with the 0.5 weighting, but I suspect if that were the case then the button example wouldn't work either, but it works perfectly. I've tried putting a small (30x30) image on and that works fine with correct sizing, so it seems the size of the ImageView is the problem. – Dororo Jul 26 '11 at 23:49
  • well, in general though thats how weighting works. I would try changing the layout_height of your image to wrap_content as well. I often find that layout design can be very finicky with different xml options. good luck ! – Adam Storm Jul 27 '11 at 03:24