【问题标题】:Android: using image instead of text on a buttonAndroid:在按钮上使用图像而不是文本
【发布时间】:2012-10-08 18:51:30
【问题描述】:

我一直在研究我的 android 计算器,但我似乎无法在按钮上插入图像而不是文本。例如 sqrt 我不想在我的按钮上写 sqrt 我想要符号的图像,x^y 和许多其他人也是如此。我的按钮的自定义背景工作得很好。提前感谢您的帮助:)

编辑: 那不是我想要的,我确实设法获得了我的自定义按钮,这很好。在我的按钮上,因为它是一个计算器按钮,所以我有 android:text 上面写着 sqrt。我想删除它并在我的按钮上插入另一个 png,显示平方根的符号而不是那个文本。感谢您的快速回复。

【问题讨论】:

  • 所以从Button 更改为ImageButton,你可以为你的图片设置背景或src 属性

标签: android image button insert


【解决方案1】:

您可以通过 ImageButton 做到这一点:

        <ImageButton
            android:id="@+id/submitEmailButton"
            android:layout_height="wrap_content" android:layout_width="wrap_content"
            android:layout_gravity="right" android:layout_marginRight="15dip"
            android:background="@drawable/ic_submit" />

这里 ic_submit 是您要显示为按钮的图像。

【讨论】:

    【解决方案2】:

    我使用了本教程,它非常适合我:http://www.mkyong.com/android/android-imagebutton-selector-example/

    我还没有看到你的代码,但基本上你应该从你的 xml 中删除“文本”标签,然后它应该会出现没有文本的图像。

    我不知道这是否正是您想要的,但我建议您按照本教程进行操作,并确保它对您有用 ;)

    【讨论】:

      【解决方案3】:
      <Button 
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" 
          android:drawableTop="@drawable/imageName"/>
      

      使用属性drawableTopdrawableBottomdrawableLeftdrawableRight 时,您可以同时拥有文本和图像(或没有文本)。

      要根据需要定位图像,请考虑使用:paddingToppaddingBottompaddingLeftpaddingRight

      【讨论】:

      • 如果我使用该符号将不在中心:(
      【解决方案4】:

      您可以尝试实施多种替代方案来构建您的 SQRT 按钮:

      • 将按钮绘制为 9-patch 可绘制对象,这样您的 SQRT 符号就不会变形;
      • 使用内部带有按钮的 RelativeLayout 并实现选择器

      【讨论】:

        【解决方案5】:

        您可以简单地为按钮设置背景并将其文本保留为空白。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-07-06
          • 2020-04-28
          • 1970-01-01
          • 2011-06-03
          • 1970-01-01
          • 2016-05-10
          • 2015-09-09
          相关资源
          最近更新 更多