【问题标题】:Android ImageButton with Image and Text带有图像和文本的 Android ImageButton
【发布时间】:2011-03-14 15:54:18
【问题描述】:

我想要一个顶部有图像,底部有一些文字的按钮。图像和文本都是在运行时决定的,所以我希望能够为每个按钮组合 ImageButton 的 setImageBitmap 和 Button 的 setText。

有什么想法吗?

谢谢 克里斯

【问题讨论】:

标签: android image text button imagebutton


【解决方案1】:

将图像按钮和文本视图代码包围在 .... 中以覆盖图像按钮背景上的文本。即:

 <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="69dp"
            android:layout_marginTop="96dp"
            android:background="@drawable/icon"
            android:scaleType="fitXY" />

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Droid"
            android:textColor="#ff0000"
            android:textSize="24dip" >
        </TextView>
    </FrameLayout>

【讨论】:

    【解决方案2】:

    我终于找到了我要找的东西:

    setCompoundDrawablesWithIntrinsicBounds 让我可以为 Button 分配位图/drawable,同时让我使用 setText。

    【讨论】:

    • 那是用于带有文本和图像的按钮,而不是 ImageButton,因此这还不是原始问题的解决方案,而是一种解决方法。
    【解决方案3】:

    例如:要在按钮顶部设置位图图像,请执行以下操作

    button.setCompoundDrawablesWithIntrinsicBounds(null, new BitmapDrawable(bitmapimage), null, null);

    【讨论】:

      【解决方案4】:

      我认为没有一种简单的方法可以做到这一点。

      我会创建一个custom-component。

      【讨论】:

      • 通过 XML 布局可以完美定义
      • @ognian:您可以使用 XML 布局来定义自定义组件。
      • 是的,但我想说的是,没有必要创建另一个 View 子类只是为了拥有一个带有图像和文本的按钮
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-04
      • 2012-09-03
      • 2011-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多