1

在我的 Fragment XML 中,我有一个用作背景的图像视图。在同一个布局中,我得到了一个 LinearLayout,它包含一些应该在视图上水平查看的按钮,并且与背景具有相同的宽度。由于 wrap_content,我的代码将其大小调整为屏幕宽度。如何将其调整到 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>

我尝试以编程方式获取宽度并分配新的布局参数,但返回 0,因为视图尚未呈现。

4

1 回答 1

1

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

于 2013-01-17T09:43:15.733 回答