How to set height of one layout component to the same height of another component?
Asked Answered
H

3

9

How can I set one layout components height to the same as another component: example: Can I set android:layout_height="@+id/info_box.height" or do something similar?

I want the height of the ImageView to match that of my LinearLayout

    <ImageView android:id="@+id/border"
    android:src="@drawable/frame"
    android:layout_below="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="????"    
    android:scaleType="fitXY" 
    android:drawingCacheQuality="auto"
    />

    <LinearLayout
    android:id="@+id/info_box"
    android:orientation="vertical"
    android:layout_below="@+id/title"
    android:background="@layout/my_bg"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    ... other stuff . .
     </LinearLayout>
Hiragana answered 13/1, 2011 at 20:11 Comment(6)
Umm. Why don't you put your imagview IN your linear layout and set the height as fill_parent?Humility
@Falmarri: If his ImageView and LinearLayout are within a horizontal LinearLayout (not specified) that wouldn't have the same result. Also, can you have fill_parent for a child, and wrap_content for the parent? That seems almost paradoxical.Fukuoka
More information is required. What is the root ViewGroup your ImageView and LinerLayout are in? What do you want to achieve with your layout?Unbalanced
It's a simple question, can I set one component's height equal to another component, even if they are not in the same container. I don't need an alternate way of laying out the components.Hiragana
Well then that particular answer is no. There's no direct way to do it as you're suggesting. If you're up for alternate suggestions to accomplish a similar result, then post more information.Fukuoka
The only way I know is using RelativeLayout and its relative alignmentsWindstorm
O
5

The only way to achieve this is to have the two views be part of the same container. A LinearLayout or RelativeLayout are usually well suited for this. You could also achieve this through code.

Overside answered 14/1, 2011 at 2:58 Comment(2)
How would you do it if they were part of the same container? I think he or she wants to make the ImageView scale to the height of the LinearLayout.Coleville
Nevermind, I found it. On the imageview: scaletype="centercrop", layout_height="fill_parent", and layout_width="fill_parent". The key? scaletype="centercrop" does not work on the layout editor.Coleville
B
1

You can use android:layout_weight.

Put your two views (border and info_box) with fill_parent and same value layout_weight in same LinearLayout with orientation=verticale.

With this technique, border and info_box will have same height.

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

    <ImageView
        android:id="@+id/border"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/title"
        android:layout_weight="2"
        android:drawingCacheQuality="auto"
        android:scaleType="fitXY"
        android:src="@drawable/frame" />

    <LinearLayout
        android:id="@+id/info_box"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/title"
        android:layout_weight="2"
        android:background="@layout/my_bg"
        android:orientation="vertical" >

        ... other stuff . .

    </LinearLayout>

</LinearLayout>
Balderas answered 23/11, 2012 at 17:10 Comment(2)
he doesn't want all the views with fill_parent. He wants one to be wrap_content, and the other to be as high as the first one.Folse
He can change width and height of first LinearLayout and choose size he want. But ImageView and second LinearLayout will always be same height (or width), because they have the same layout_weight..Balderas
B
0

In RelativeLayout simply add these lines to your "element_with_the_same_height":

android:layout_alignBottom="@+id/element_with_needed_height"
android:layout_alignTop="@+id/element_with_needed_height"

of course, in this case elements will be on the same level

Briannabrianne answered 17/12, 2022 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.