开发者

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

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&q开发者_JS百科uot;
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    ... other stuff . .
     </LinearLayout>


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.


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>


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜