Android:将 ImageView (src) 的初始大小保留为响应式 LinearLayout(不带 dp)



>上下文

我希望在Android上创建一个棋盘游戏(如国际象棋),其全屏响应板与任何屏幕(横向)兼容,使用LinearLayout包括match_parentwrap_content以及weightSumlayout_weight的复杂组合,无论屏幕如何,都能保持相同的外观:

  • 查看上下文屏幕截图

问题

如您所见,它工作正常,直到最后一个子元素被ImageView但是当我尝试放置backgroundsrc的那一刻,ImageView元素的大小完全改变并破坏了板的外观; 而foreground不会造成任何问题:

  • (确定)使用前景查看屏幕截图
  • (KO) 查看带有背景或 src 的屏幕截图

尝试

foregroundGravity放在fill上,无论drawable类型如何,都适用于foreground资源;而scaleType(fitCentercenterCrop等)、layout_gravity(fillcenter等)或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,当它在backgroundsrc上使用时不会改变盒子的外观,但在视觉内容方面仍然受到限制;我想知道是否有线索可以遵循强制其他类型的资源(矢量、png 等)的行为?

例如,这种drawablebackgroundsrc上时工作,而无需调整大小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放在我的ImageViewsrc而不调整它的大小吗?当然,使用 HTML/CSS/JS 会更容易做到,太糟糕了!

非常感谢您的任何想法!

Android 是如何工作的?

Android 似乎以特定顺序应用视图属性,即与其父容器关联的我的ImageViewlayout_weight比它自己的大小参数次要。

因此,如果ImageViewlayout_widthlayout_height(取决于您LinearLayoutorientation)wrap_content并且此内容大于ImageView,其最终大小将与其内容一样大,并且在我的情况下肯定会更大。


问题已解决

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

如果您应用layout_weight,您也可以简单地将ImageViewlayout_width(或layout_height)强制为 0dp 。


我感谢昆汀和彼得的建议,希望这个解决的问题对您有所帮助!

最新更新