简体   繁体   中英

Size LinearLayout to the Width of ImageView

In my Fragment XML i have an imageview which functions as a background. In that same layout i got a LinearLayout which holds some buttons that should be viewed horizontally on the view and have the same width as the background. My code sizes it to the width of the screen because of wrap_content. How can I size it to the imageview?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:layout_marginTop="45sp" >

<ImageView 
        android:src="@drawable/background_detail_view"
        android:id="@+id/detailview_background"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/detailview_background_description" />

<!-- more widgets -->

<LinearLayout
        android:id="@+id/detail_button_container"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_above="@+id/titel" >

        <Button 
        android:id="@+id/detail1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="1"/>

<Button 
        android:id="@+id/detail2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="2"/>

        <Button 
        android:id="@+id/detail3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="3"/>

<!-- more buttons -->

</LinearLayout>

I tried getting the width programatically and assigning that tot new layoutparams but that returns 0 because the view isnt rendered yet.

在您的LinearLayout中,设置android:background="@drawable/background_detail_view"参数,以便您的LinearLayout具有此背景,而不是使用ImageView。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM