【问题标题】:Resize ImageButton?调整图像按钮的大小?
【发布时间】:2011-07-04 23:27:28
【问题描述】:

我有一个 ImageButton,我正在使用包含的 @android:drawable/ic_menu_more 图像。但它太大了。调整大小以使其更好地适合我的表单的最佳方法是什么?另外,它可以在 xml 中旋转吗? (仅一次,不基于状态)

【问题讨论】:

    标签: android


    【解决方案1】:

    尝试设置android:background 而不是android:src 来设置按钮上的图像。这可能对您的情况有所帮助,因为它会将图像拉伸到您的按钮大小。此外,您必须为按钮指定固定尺寸(使用dip 而不是px)。示例:

    <ImageButton
         android:background="@drawable/ic_menu_more"
         android:layout_width="50dip"
         android:layout_height="50dip" />
    

    【讨论】:

    • 对于那些以编程方式执行此操作的人,请使用 setbackgrounddrawable 或 setbackground 而不是 setimageresource
    • 我现在在更改图标颜色时遇到问题:android:tint="#D50000" 不起作用,而 android:backgroundTint="#D50000" 在 v21 以下不可用
    【解决方案2】:

    根据 steven-byle 的解决方案 (https://stackoverflow.com/a/15117536/1741997),他说:

    "...使用 android:scaleType="fitCenter" 让 Android 缩放图像,使用 android:adjustViewBounds="true" 让它们根据缩放调整边界..."

    对我有用

    【讨论】:

      【解决方案3】:

      您可以使用 NinePatchDrawable... 可调整大小的位图,具有您定义的可拉伸区域。

      http://developer.android.com/reference/android/graphics/drawable/NinePatchDrawable.html

      【讨论】:

      • 我不明白。我正在尝试为我的按钮使用系统映像。
      【解决方案4】:

      我解决此问题的方法是从相关绘图资源创建位图,然后将其转换为具有所需大小的缩放位图。我还将背景设置为透明以仅显示图像。

          int height = 300;
          int width = 500
          Bitmap bmp;
          bmp = BitmapFactory.decodeResource(getResources(), R.drawable.myresourcename);
          bmp = Bitmap.createScaledBitmap(bmp, width, height, true);
          ImageButton imageButton = new ImageButton(this.getActivity());
          imageButton.setImageBitmap(bmp);
          imageButton.setBackgroundColor(Color.TRANSPARENT);
      

      然后将其添加到布局中。

      【讨论】:

        【解决方案5】:

        应用cesargastonec's 答案将使图像适合您的 ImageButton,没有太多空间。如果您使用的是 png,则可以将图像移动到更大的背景以获得更小的图像

        例如,在 android:scaleType="fitCenter" 的 ImageButton 中,将 256x256 背景中的 48x48 图像移动到 512x512 背景中的大小将减半。

        这种方法最适用于您打算仅在 ImageButtons 中使用的图像,因为它可能会在其他地方笨拙地缩放并且具有看似大的点击框。

        【讨论】:

          猜你喜欢
          • 2022-12-09
          • 1970-01-01
          • 2014-05-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-17
          • 1970-01-01
          • 2013-03-17
          相关资源
          最近更新 更多