【问题标题】:Using LinearLayout to display a variable number of images使用 LinearLayout 显示可变数量的图像
【发布时间】:2017-05-26 15:00:29
【问题描述】:

在 LinearLayout 中,我尝试使用图像作为数字显示整数。我希望数字居中,图像根据存在的位数缩放。

我的初始布局是 2 位数字:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<view
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal"
    android:gravity="center"
    >
    <ImageView
        app:srcCompat="@drawable/ic_digit_0"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:layout_weight="1"
        />
    <ImageView
        app:srcCompat="@drawable/ic_digit_0"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:layout_weight="1"
        />
</LinearLayout>

这就是它的样子。

A) 有 4 位数字,一切看起来都不错,数字水平适合,垂直方向尽可能高。

B) 有了更多的数字,它仍然可以很好地扩展到可用空间。

C) 但是当LinearLayout高度减小时,4位数字会分开,它们之间会出现间隙。

D) 通过从每个 ImageView 中删除 layout_weight,这 4 个数字会重新组合在一起。

E) 但是当 LinearLayout 高度增加时,数字会缩放到视图的整个高度,并且一些数字会水平消失在屏幕之外。

无论LinearLayout尺寸如何,如何调整这些数字图像的大小以填充可用空间、显示所有数字并无间隙居中,同时保持它们的比例比例?

谢谢。

【问题讨论】:

  • 你试过 android:scaleType 根据你的要求试试它的属性
  • 给定大小的imageview!使用 scaletype-
  • scaleType 似乎不起作用。 fitXY 拉伸或挤压图像,使它们的比例改变比例。固定图像大小需要根据位数变化的代码计算。
  • 尝试使用 android:scaleType="centerInside" 和 android:scaleType="fitCenter"
  • 我已经尝试了所有的 scaleType 值,包括:center、centerInside、centerCrop、fitCenter、fitEnd、fitXY 和 matrix。

标签: android android-layout imageview android-linearlayout


【解决方案1】:

您是否尝试过添加 scaletype?

<ImageView android:scaleType="scaleXY" android:src="@drawable/my_image"/>

https://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType

【讨论】:

【解决方案2】:

好的。我找到了答案。

对于第二个 LinearLayout,将 android:layout_width= 从 "ma​​tch_parent" 更改为 "wrap_content" .然后添加 android:layout_gravity="center"。这允许 LinearLayout 在较小的高度迫使数字缩小时缩小到其内容。但是,当它缩小时,它也会在包含视图中居中。

感谢大家的意见。

【讨论】:

    猜你喜欢
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多