1

我不明白 ImageView 大小如何在定义了权重的相对布局上工作

这是我的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".CameraActivity" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="76" >

        <ImageView
            android:id="@+id/imagePreview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:src="@drawable/photo_placeholder" />

        <Button
            android:id="@+id/timeButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:onClick="onClick"
            android:text="Button" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="24" >

        <ImageButton
            android:id="@+id/mainButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:background="#0fff"
            android:src="@drawable/cam_main_btn_normal" />

        <ImageButton
            android:id="@+id/nextButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="22dp"
            android:background="#0fff"
            android:src="@drawable/gs_05_check_normal" />

        <ImageButton
            android:id="@+id/cancelButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="22dp"
            android:background="#0fff"
            android:src="@drawable/cam_cancel" />
    </RelativeLayout>

</LinearLayout>

我明白 错误的结果 了,但如果我删除图像,它会按照我的预期正确显示尺寸 没有 imageView 的结果

所以问题是,我应该怎么做才能让 imageView 在相对布局的权重方面占据全部空间?

我试图将 imageView 的 android:layout_width, android:layout_height 更改为 wrap_content (没有意义,但只是检查)它不起作用。我尝试了使用和不使用 scaleType,并且不同的值不会改变。

我猜 ImageView 需要一些技巧/魔法?

-- 编辑 -- 我希望图像将在第二个屏幕截图中采用蓝色突出显示的大小:) -- 编辑 -- 我想要实现的目标: 我想要达到的结果

4

2 回答 2

0

仍然没有得到你正在尝试的东西,你的体重工作方式可能是错误的。但试试这个演示,希望它能帮助你了解重量。

只需将其粘贴到 xml 中。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight=".80" >

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/ic_launcher" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight=".20" >

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="33.33"
            android:background="@drawable/ic_launcher" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="33.33"
            android:background="@drawable/ic_launcher" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="33.33"
            android:background="@drawable/ic_launcher" />
    </LinearLayout>

</LinearLayout>

如果对重量有任何疑问,请询问。

于 2013-07-03T12:15:39.507 回答
0

事实上,代码很好,它更像是一个日食“预览”错误,因为它可以在设备上运行(之前没有机会测试)

对不起大家

于 2013-07-03T12:11:54.917 回答