0

I've tried a couple different approaches with the weights, but obviously there is some glaring mis-understanding of how to get what I am aiming for here. Simply what I am attempting to do, is have the ImageView take up 1/3rd of the screen across the width, while having the layout of textViews take up the remainder of the 2/3rds of the screen across the width.

What ends up having as I try and manipulate it however, is that the ImageView is small and not taking up nearly the space that it should be. I've been messing around with trying to get the layouts right all morning.

The answer below has led me to the following which was what was occurring in this instance:

0dip layout dimensions

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:weightSum="3">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/skeleton" 
            android:layout_weight="1"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" 
            android:layout_weight="2">

            <EditText
                android:id="@+id/editText1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10" />

            <EditText
                android:id="@+id/editText2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>
Community
  • 1
  • 1
Jay Snayder
  • 4,298
  • 4
  • 27
  • 53
  • possible duplicate of [Android Layout Weight](http://stackoverflow.com/questions/4986861/android-layout-weight), specifically this answer: http://stackoverflow.com/a/4987097/752320 – Geobits Jul 31 '13 at 15:11

3 Answers3

3

In linear layouts, there is a trick to make weights work - you should set dimension to apply weight to zero.

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:src="@drawable/display_pro_skeleton" 
    android:layout_weight="1"/>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:layout_weight="2">

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:ems="10" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:ems="10" />

</LinearLayout>

Something like this.

Raiv
  • 5,731
  • 1
  • 33
  • 51
1

There's nothing really tricky about weights at all. What using weights says is "measure all the views in my layout, then take whatever space is left over, and divide it according to their weights". Weights only affect leftover space. This is why setting the dimensions of all the views to 0dp works, because in that case, all the space is leftover space.

yarian
  • 5,922
  • 3
  • 34
  • 48
  • That was the trick part. I was under the assumption that 'wrap_content' was similar to how '0dip' would work. That it was waiting for input from either a weight or dimensions from the source such as image width/height. – Jay Snayder Jul 31 '13 at 17:43
  • 1
    Oh I see. No, wrap_content just assigns it the minimum size that could fit its contents. Glad to hear it's all cleared up now. – yarian Jul 31 '13 at 18:49
0

Weights are somewhat counter-intuitive at times. Try using decimals so the big one will be .66 and the small one will be .34. There are many examples out there that use whole numbers but I believe that in most cases it is better to use numbers between 0 and 1.

EDIT: So I just looked a little more at your xml and realized it's probably more because of the width and heights you are using. For best results with weights you should use "fill_parent."

user2483079
  • 533
  • 2
  • 10