【问题标题】:Size LinearLayout to the Width of ImageView将 LinearLayout 调整为 ImageView 的宽度
【发布时间】:2012-12-31 19:29:36
【问题描述】:

在我的 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。

【问题讨论】:

    标签: java android xml android-layout android-widget


    【解决方案1】:

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

    【讨论】:

    • jeah.. 好主意。需要更改布局,因为很多内容是在“ImageView 下方”定义的,并且必须重新组织。我会及时通知你
    猜你喜欢
    • 2013-02-17
    • 1970-01-01
    • 1970-01-01
    • 2016-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多