【问题标题】:Unable to resize images in LinearLayout and Weight property set无法在 LinearLayout 和 Weight 属性集中调整图像大小
【发布时间】:2016-03-01 20:39:54
【问题描述】:

我正在尝试调整图像大小并增加图标大小,但是我已将它们添加到 LinearLayout 并且每 5 个图标共享权重。它们自己适合,因此我无法增加它们的图标大小/高度/宽度。

快照:

这是我的 XML:

<LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#ffffff"
            android:orientation="horizontal"
            android:weightSum="5">

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:background="@null"
                android:src="@drawable/feed_blue" />

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:background="@null"
                android:src="@drawable/compass" />

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:background="@null"
                android:src="@drawable/circle_blue" />

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:background="@null"
                android:src="@drawable/heart_beat" />

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:background="@null"
                android:src="@drawable/user_icon_female" />

        </LinearLayout>

我需要根据需要增加/减小图标的大小,但 Weight 属性可能会限制该功能。伙计们,我怎样才能做到这一点?

【问题讨论】:

  • ScaleType 到 centerInside 并在 pngs 中固定你想要的大小。我认为这些图标的高度和宽度应该是 24dp。检查材料设计指标。
  • 您只需要增加一个imageButton的大小还是同时增加所有的?
  • 可能全部或可能一个,取决于不同的图标配合@burakKarasoy
  • @Earthling 添加了一个选项

标签: java android android-layout android-activity android-studio


【解决方案1】:
<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffffff"
            android:orientation="horizontal"
            android:weightSum="5">

            <ImageButton
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@null"
                android:src="@drawable/feed_blue" />

            <ImageButton
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@null"
                android:src="@drawable/compass" />

            <ImageButton
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@null"
                android:src="@drawable/circle_blue" />

            <ImageButton
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@null"
                android:src="@drawable/heart_beat" />

            <ImageButton
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@null"
                android:src="@drawable/user_icon_female" />

        </LinearLayout>

在这种情况下,它们都占总宽度的 %20%,您可以通过更改重量轻松更改宽度。您还可以使用 padding 属性更改宽度和高度。

【讨论】:

    【解决方案2】:

    对于普通应用,您需要为所有 android 资源提供图像包,例如 mipmap-hdpi、mipmap-mdpi、mipmap-nodpi、mipmap-xhdpi、mipmap-xxhdpi、mipmap-xxxhdpi,您不应更改以编程方式设置图像大小,如果您没有图像包,请尝试在所有按钮中播放 vs android:scaleType,例如 android:scaleType="centerCrop"

    【讨论】:

    • mipmap 文件夹中除了启动器图标外,其他任何东西都不应放置。
    • 我没看错 :) 用户看不到细节上的差异。官方文档指出 mipmap 仅用于启动器图标,因为当设备的密度为 xxhdpi 时,某些启动器需要例如 xxxhdpi res。如果您担心性能和内存,请改用 SVG。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-07
    • 2018-06-17
    • 2011-06-28
    • 1970-01-01
    • 2014-08-17
    • 2012-08-18
    • 2018-06-07
    相关资源
    最近更新 更多