0

I have the following layout, and for some reason the ScrollView overlaps the over views. I don't quite understand the whole use of the weight and layout_weight attribute in android. I am thinking that has something to do with my problem.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/topbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
         />

    <ImageView
        android:id="@+id/secondbar"
        android:adjustViewBounds="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/topbarr"
        android:scaleType="fitXY"

         />

    <ScrollView
        android:id="@+id/buttonlistview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_below="@+id/secondbar"
        android:scrollbarFadeDuration="0"
         >
    </ScrollView>



</RelativeLayout>
odds0cks
  • 141
  • 4
  • 11
  • your problem is not cause by weight (refer to answers below by others), but if you wish to know more about them, you can refer to here http://stackoverflow.com/questions/3995825/what-does-androidlayout-weight-mean – ipohfly Mar 06 '14 at 09:58
  • First thing is that in Relative layout weight property is not working and if you want to add than you can use linear layout and maintain weight for multi support functioning. – Tarun - Systematix Mar 06 '14 at 10:00

3 Answers3

1

Firstly, you should have only one view with android:layout_alignParentTop="true" if you want your views to be one below another.

Secondly, you only need to use @+id once for each variable. Once it's declared, you refer to it by @id.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/topbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
         />

    <ImageView
        android:id="@+id/secondbar"
        android:adjustViewBounds="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/topbar"
        android:scaleType="fitXY"

         />

    <ScrollView
        android:id="@+id/buttonlistview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/secondbar"
        android:scrollbarFadeDuration="0"
         >
    </ScrollView>

</RelativeLayout>
Szymon
  • 42,577
  • 16
  • 96
  • 114
1

android weight and layout_weight attributes can be only used for Linearlayout or its childLayout. As for layout_weight is used with the child view of the LinearLayout which divides the child width or height in ratios. if u give layout_weight you should set the corresponding width or height to 0dp. layout_weightSum is used with the parent the Linearlayout to define how much ratio it can be divided. This hasn't to do anything with your problem right now, because you are using a RelativeLayout all the childViews start from the topleft corner. you have to give constraints in relativeLayout to align your views

And for scrollview A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.

You should never use a ScrollView with a ListView, because ListView takes care of its own vertical scrolling.

Vishnu Prabhu
  • 449
  • 1
  • 5
  • 19
0

take linear layout and adjust the weight as your desired UI looks here it will divide three equal halves for 2 imageview and scrollview

<ImageView
    android:id="@+id/topbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_weight="1"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    android:src="@drawable/ic_launcher"
     />

<ImageView
    android:id="@+id/secondbar"
    android:adjustViewBounds="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/topbarr"
    android:scaleType="fitXY"

     />

<ScrollView
    android:id="@+id/buttonlistview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_below="@+id/secondbar"
    android:scrollbarFadeDuration="0"
     >
</ScrollView>

Santhosh
  • 1,867
  • 2
  • 16
  • 23