Android: 将 ImageView (src) 的初始大小保留到响应式 LinearLayout(无 dp)中

Android: keep initial size of ImageView (src) into a responsive LinearLayout (without dp)

上下文

我想在 Android 上创建一个棋盘游戏(如国际象棋),并使用 LinearLayout 包括 match_parentwrap_content 以及 [=16= 的复杂组合]layout_weight 无论屏幕如何都保持相同的外观:


问题

如您所见,它在最后一个子元素 ImageView 之前工作正常,但是当我尝试将 backgroundsrcImageView 元素的大小完全改变并破坏了董事会的外观;而 foreground 不会造成任何问题:


尝试

foregroundGravity 放在 fillforeground 任何类型的资源 drawable;而 scaleType 的任何参数 (fitCenter, centerCrop, 等), layout_gravity (fill, center, 等) 或 adjustViewBounds 似乎没有给出预期的结果并更改了框的大小。

你可以用我下面的 XML 代码自己试试:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/linearScreen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/field"
    android:orientation="vertical"
    android:weightSum="8800">

    <LinearLayout
        android:id="@+id/linearHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1000"
        android:baselineAligned="false"
        android:orientation="horizontal">

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearFieldC"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="745"
        android:baselineAligned="false"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="0dp"
        android:weightSum="15640">

        /* some ImageView */

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearFieldB"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="760"
        android:baselineAligned="false"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="0dp"
        android:weightSum="15640">

        /* some ImageView */

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearFieldA"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="780"
        android:baselineAligned="false"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="0dp"
        android:weightSum="15640">

        /* some ImageView */

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearField0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="800"
        android:baselineAligned="false"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="0dp"
        android:weightSum="15640">

        <ImageView
            android:id="@+id/goal0F"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="919"
            android:contentDescription="@string/empty"
            android:foregroundGravity="fill"
            android:foregroundTintMode="multiply"
            android:padding="0dp">

        </ImageView>

        <ImageView
            android:id="@+id/square0E"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="919"
            android:contentDescription="@string/empty"
            android:foregroundGravity="fill"
            android:foregroundTintMode="multiply"
            android:padding="0dp">

        </ImageView>

        <ImageView
            android:id="@+id/square0D"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="919"
            android:contentDescription="@string/empty"
            android:foregroundGravity="fill"
            android:foregroundTintMode="multiply"
            android:padding="0dp">

        </ImageView>

        <ImageView
            android:id="@+id/square0C"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="919"
            android:contentDescription="@string/empty"
            android:foregroundGravity="fill"
            android:foregroundTintMode="multiply"
            android:padding="0dp">

        </ImageView>

        <ImageView
            android:id="@+id/square0B"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="919"
            android:contentDescription="@string/empty"
            android:foregroundGravity="fill"
            android:foregroundTintMode="multiply"
            android:padding="0dp">

        </ImageView>

        <ImageView
            android:id="@+id/square0A"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="919"
            android:contentDescription="@string/empty"
            android:foregroundGravity="fill"
            android:foregroundTintMode="multiply"
            android:padding="0dp">

        </ImageView>

        <ImageView
            android:id="@+id/square00"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="919"
            android:contentDescription="@string/empty"
            android:foregroundGravity="fill"
            android:foregroundTintMode="multiply"
            android:padding="0dp">

        </ImageView>

        <ImageView
            android:id="@+id/square01"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="919"
            android:contentDescription="@string/empty"
            android:foregroundGravity="fill"
            android:foregroundTintMode="multiply"
            android:padding="0dp">

        </ImageView>

        <ImageView
            android:id="@+id/square02"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="919"
            android:contentDescription="@string/empty"
            android:foregroundGravity="fill"
            android:foregroundTintMode="multiply"
            android:padding="0dp">

        </ImageView>

        <ImageView
            android:id="@+id/square03"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="919"
            android:contentDescription="@string/empty"
            android:foregroundGravity="fill"
            android:foregroundTintMode="multiply"
            android:padding="0dp">

        </ImageView>

        <ImageView
            android:id="@+id/square04"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="919"
            android:contentDescription="@string/empty"
            android:foregroundGravity="fill"
            android:foregroundTintMode="multiply"
            android:padding="0dp">

        </ImageView>

        <ImageView
            android:id="@+id/square05"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="919"
            android:contentDescription="@string/empty"
            android:foregroundGravity="fill"
            android:foregroundTintMode="multiply"
            android:padding="0dp">

        </ImageView>

        <ImageView
            android:id="@+id/goal06"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="919"
            android:contentDescription="@string/empty"
            android:foregroundGravity="fill"
            android:foregroundTintMode="multiply"
            android:padding="0dp">

        </ImageView>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearField1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="840"
        android:baselineAligned="false"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="0dp"
        android:weightSum="15640">

        /* some ImageView */

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearField2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="850"
        android:baselineAligned="false"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="0dp"
        android:weightSum="15640">

        /* some ImageView */

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearField3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="855"
        android:baselineAligned="false"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="0dp"
        android:weightSum="15640">

        /* some ImageView */

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearFooter"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2170"
        android:baselineAligned="false"
        android:orientation="horizontal"></LinearLayout>
</LinearLayout>

但是,受 Android 按钮的启发,我制作了一个 drawable,它在 [=76] 上使用时不会改变框的外观=]backgroundsrc 但在视觉内容方面仍然有限;不知道有没有什么线索可以强制其他类型资源(vector、png等)的行为?

比如这种drawable放在backgroundsrc 不调整大小 ImageView;在 0 处使用 padding 和透明度 stroke 模拟虚拟 padding :

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="0dp"
    android:insetTop="0dp"
    android:insetRight="0dp"
    android:insetBottom="0dp">
    <ripple android:color="@android:color/black">
        <item>
            <shape android:shape="oval">
                <solid android:color="#26000000"/>
                <stroke
                    android:color="#00000000"
                    android:width="8dp">
                </stroke>
                <padding
                    android:left="0dp"
                    android:top="0dp"
                    android:right="0dp"
                    android:bottom="0dp">
                </padding>
            </shape>
        </item>
    </ripple>
</inset>

帮助

你能帮我把drawable放到我src中吗ImageView 不调整大小?当然,用HTML/CSS/JS会更简单,可惜了!

非常感谢任何想法!

Android 是如何工作的?

Android 似乎以特定顺序应用视图属性,使得我的 [=11= 的 layout_weight ],与其父容器关联,比其自身的大小参数次要。

所以如果ImageViewlayout_width或者layout_height(取决于你的 LinearLayout orientation)是 wrap_content 并且此内容大于 ImageView,其最终大小将与其内容一样大,并且在我的情况下肯定更大。


问题已解决

知道了这一点,您可以为您的内容制作一个尽可能小的 drawable,这样它就可以填充 ImageView(仅适用于矢量文件,不建议使用位图资源)。

您也可以简单地强制 ImageView layout_width(或 layout_height) 在 0dp 如果你应用 layout_weight.


感谢 Quentin 和 Peter 的建议,希望这个已解决的问题对您有所帮助!