jrmgx
jrmgx

Reputation: 729

LinearLayout with RelativeLayout having imageView inside don't respect weight constraint

I don't understand how the ImageView size works on a relative layout having weight defined

This is my code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".CameraActivity" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="76" >

        <ImageView
            android:id="@+id/imagePreview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:src="@drawable/photo_placeholder" />

        <Button
            android:id="@+id/timeButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:onClick="onClick"
            android:text="Button" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="24" >

        <ImageButton
            android:id="@+id/mainButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:background="#0fff"
            android:src="@drawable/cam_main_btn_normal" />

        <ImageButton
            android:id="@+id/nextButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="22dp"
            android:background="#0fff"
            android:src="@drawable/gs_05_check_normal" />

        <ImageButton
            android:id="@+id/cancelButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="22dp"
            android:background="#0fff"
            android:src="@drawable/cam_cancel" />
    </RelativeLayout>

</LinearLayout>

and I get Wrong result but if I remove the image it shows dimentions correctly as I expect Result without imageView

So the question is, how should I do to get the imageView to take the full space respecting the weight on the relative layout?

I tried to change the android:layout_width, android:layout_height of the imageView to wrap_content (make no sense, but just' checking) it doesn't work. I tried with and without scaleType, and different values, doesn't change.

I guess ImageView needs some trick/magic?

-- EDIT -- I'm expecting that the image will take the size highligted in blue on the second screenshoot :) -- EDIT -- What I want to achieve: The result I want to achieve

Upvotes: 1

Views: 2030

Answers (2)

Rumit Patel
Rumit Patel

Reputation: 12469

still not getting what you are trying,your way to working with weight is may be wrong. but try this demo, hope it will help you to understand weight.

just paste it into xml.

<?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" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight=".80" >

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/ic_launcher" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight=".20" >

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="33.33"
            android:background="@drawable/ic_launcher" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="33.33"
            android:background="@drawable/ic_launcher" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="33.33"
            android:background="@drawable/ic_launcher" />
    </LinearLayout>

</LinearLayout>

still if any query about weight,then plz ask.

Upvotes: 0

jrmgx
jrmgx

Reputation: 729

In fact that code was good and it's more of an eclipse "preview" bug as it works on device (did not have a chance to test before)

Sorry all

Upvotes: 0

Related Questions