【问题标题】:LinearLayout with RelativeLayout having imageView inside don't respect weight constraint具有内部具有 imageView 的 RelativeLayout 的 LinearLayout 不遵守重量约束
【发布时间】:2013-06-29 20:49:06
【问题描述】:

我不明白 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 的 android:layout_width, android:layout_height 更改为 wrap_content (没有意义,但只是检查)它不起作用。 我尝试了使用和不使用 scaleType,并且不同的值不会改变。

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

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

【问题讨论】:

  • android:weightSum=100 在父 LinearLayout 上?
  • 简单的View也存在这个问题
  • @DavidOlsson 不幸的是我已经尝试过了,但它不起作用。
  • @gigigan 你能给我 photo_placeholder 和 gs_05_check_normal 图像吗?我想测试一下。我没有它测试过似乎都是正确的。
  • 我明白了。我不知道这是否会有所作为,但您应该使用 dp 而不是 px 来代替 height

标签: android android-layout android-imageview


【解决方案1】:

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

对不起

【讨论】:

    【解决方案2】:

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

    只需将其粘贴到 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>
    

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

    【讨论】:

    • 上相对布局为 80%,lawar 线性布局为 20%。与律师相同的版式有3个相同的部分33.33%。
    • 理解layout_width的好例子。+1兄弟的好例子。
    • 0dp 高度对我有用。 Android 有时就是这么愚蠢。
    猜你喜欢
    • 2017-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多