Android:ImageView src忽略父元素的layout_weight属性

时间:2019-06-24 13:49:40

标签: android android-layout

我有两个具有属性layout_weight = "1"的垂直表行,因此它们在垂直方向上占据相等的空间,但是如果我在具有android:src属性的那些表标签中添加“ ImageView”标签,则会调整父tableRow标签的大小根据图像的实际大小,有什么办法可以使TableRow保持相等的高度,而无需指定ImageView或TableRow的确切大小 代码

工作代码(简化版)

<?xml version="1.0" encoding="utf-8"?>    
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">

<TableRow
    android:layout_width="match_parent"
    android:layout_weight="1">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/white" />
</TableRow>

<TableRow
    android:layout_width="match_parent"
    android:layout_weight="1">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/black" />
</TableRow>
</TableLayout>

但是,一旦我将android:src添加到ImageViews属性并选择一个图像,父<TableRow>的大小就会根据该图像的大小而变化,这有什么用,符合父级的当前大小,并且不更改其父级大小吗?

基于Manishika的答案,我尝试了此代码,但也无法正常工作

<TableLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:measureWithLargestChild="true"
xmlns:android="http://schemas.android.com/apk/res/android">

<TableRow
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="@android:color/holo_green_light"
    android:gravity="center"
    android:layout_weight=".5">

    <ImageView
        android:layout_width="match_parent"
        android:src="@drawable/ic_launcher_background"
        android:background="@android:color/white"/>
</TableRow>

<TableRow
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight=".5"
    android:gravity="center"
    android:background="@android:color/holo_blue_bright">

    <ImageView
        android:layout_width="match_parent"
        tools:srcCompat="@tools:sample/backgrounds/scenic"
        android:background="@android:color/black"/>
  </TableRow>
</TableLayout>

谢谢!

5 个答案:

答案 0 :(得分:0)

确保parent_layout具有android:weightSum="2"属性

  

android:weightSum

     

定义最大重量总和。如果未指定,则通过将所有子项的layout_weight相加来计算总和。这个可以   例如用于给一个孩子提供全部可用资源的50%   通过给它的layout_weight为0.5并将weightSum设置为   1.0。

答案 1 :(得分:0)

尝试一下。 这样,两行将划分表格布局高度。 我将高度指定为400dp只是为了测试它是否在行之间平均分配。您可以根据需要保留它与父项或其他项匹配。

    <TableLayout android:layout_width="match_parent"
        android:layout_height="400dp">
        <TableRow android:layout_weight="1">
            <ImageView
                android:background="@color/white"
                android:src="@drawable/image"
                android:layout_height="match_parent"/>
        </TableRow>
        <TableRow android:layout_weight="1">
            <ImageView
                android:background="@color/black"
                android:src="@drawable/image"
                android:layout_height="match_parent"/>
        </TableRow>
    </TableLayout>

答案 2 :(得分:0)

检查刻度类型方法。此外,如果您不希望图像跳至其余布局,则可以始终放置特定的高度或宽度

android:scaleType="centerCrop"

答案 3 :(得分:0)

您必须将android:weightSum="2"放在TableLayout上,并且还需要在两个TableRow中都设置layout_width="0dp"。我认为您不需要在ImageView上使用android:layout_weight="1"

答案 4 :(得分:0)

如果您仍在寻找答案,请考虑进行以下更改:

  1. 在您的android:measureWithLargestChild="true"中添加TableLayout,这将确保所有行的高度都占最大TableRow

  2. 与其将重力添加到TableLayout而不是将其添加到TableRows中,因为layout_gravity控制着视图如何将其内容布置在自己的容器中,并且我相信您希望图像居中。

  3. weightSum添加到TableLayout并将权重除以每个TableRow

尝试以下我使用且有效的布局XML:

<TableLayout android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:weightSum="1"
             android:measureWithLargestChild="true">

    <TableRow
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="@android:color/holo_green_light"
            android:gravity="center"
            android:layout_weight=".5">

        <ImageView
                android:layout_width="match_parent"
                android:src="@drawable/ic_launcher_background"
                android:background="@android:color/white"/>
    </TableRow>

    <TableRow
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight=".5"
            android:gravity="center"
            android:background="@android:color/holo_blue_bright">

        <ImageView
                android:layout_width="match_parent"
                android:src="@drawable/ic_dashboard_black_24dp"
                android:background="@android:color/black"/>
    </TableRow>
</TableLayout>