【问题标题】:Android reduce size of icon inside image viewAndroid减小图像视图内图标的大小
【发布时间】:2020-07-09 11:09:02
【问题描述】:

我在列表视图中有以下选项卡,它们在图像视图中使用图标 SVG:

我的问题是 SVG 图标非常大,我需要调整它的大小而不调整图像视图及其形状的大小,我想要这样的:

这是我的代码:

circle_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    
    <size
        android:width="160dp"
        android:height="160dp" />
    
</shape>

图像视图:

  <ImageView
        android:id="@+id/ivLearnsetType"
        android:layout_width="36dp"
        android:layout_height="29dp"
        android:layout_marginBottom="25dp"
        android:gravity="center"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:text="@string/placeholder"
        android:textColor="@color/Black" />

Java:

        Integer color = PokemonUtils.getColorForType().get(convertedType);
        Drawable drawable = getContext().getDrawable(R.drawable.ic_bug_type_icon);
        ivLearnsetType.setImageDrawable(drawable);
        ivLearnsetType.setBackgroundResource(R.drawable.circle_shape);
        GradientDrawable ivDrawable = (GradientDrawable) ivLearnsetType.getBackground();
        ivDrawable.setColor(color);

【问题讨论】:

    标签: android android-studio android-imageview android-image androidsvg


    【解决方案1】:

    在 Imageview 中使用 android:scaleType="centerInside"

    【讨论】:

      【解决方案2】:

      尝试使用android:scaleType="fitXY"

      这是完整的代码...

      <ImageView
          android:id="@+id/ivLearnsetType"
          android:layout_width="36dp"
          android:layout_height="29dp"
          android:layout_marginBottom="25dp"
          android:gravity="center"
          android:scaleType="fitXY"
          android:scrollHorizontally="true"
          android:singleLine="true"
          android:text="@string/placeholder"
          android:textColor="@color/Black" />
      

      希望这会有所帮助...随时要求澄清...

      【讨论】:

      • 图标没变,还是很大
      • @NexussimLements 使用fitXY 并尽可能更改ImageView 的大小
      【解决方案3】:

      将此添加到 app/build.gradle 以获得支持

      defaultConfig {
          vectorDrawables.useSupportLibrary = true
      }
      

      在 onCreate 方法中添加这个。

      AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-01
        • 2012-06-19
        • 2013-01-01
        • 2021-07-23
        • 2017-02-06
        • 2019-04-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多