设置ImageButton中的文本

7

我有一个看起来像这样的ImageButton。

 <ImageButton
            android:id="@+id/ImageButton"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_gravity="center"
            android:layout_marginBottom="6px"
            android:layout_marginRight="3px"
            android:layout_toRightOf="@+id/Logo"
            android:background="@drawable/button_bg_purple"
            android:scaleType="fitStart"
            android:src="@drawable/ImageButton" />


 ImageButton ImgButton= (ImageButton) this.findViewById(R.id.ImageButton);

现在我需要在ImageButton中以编程方式添加动态文本,但我不能使用按钮。我该怎么做呢?
提前致谢...

1
为什么不能使用Button?ImageButtons没有text属性,它们扩展自ImageView。如果您不想使用Button,则可以使用TextView。或者...您将不得不通过编程方式在ImageButton中绘制文本...这样会造成很大的开销! - Phantômaxx
ImageButton不能有文本! - Emad Aghaei
5个回答

15

ImageButton 不能包含文字:

您可以尝试的选项:

1)使用Button代替ImageButton

2)使用ImageButton和下方的TextView来显示文本

希望这可以帮到您


5

你无法为ImageButton设置文本,因为它没有setText()方法或android:text属性。

ImageButtons不能有文本(或者至少在其属性中未列出android:text)。看起来你需要使用Button(并查看drawableTop或setCompoundDrawablesWithIntrinsicBounds(int,int,int,int))。

尝试使用Button而不是ImageButton。你可以添加按钮背景图像。所以请尝试使用Button而不是ImageButton。


3
你可以使用drawableRightdrawableLeft属性,并使用text属性创建带有文本和图片的按钮。
<Button 
 android:id="@+id/buttonok" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content"
 android:drawableLeft="@drawable/buttonok"
 android:text="OK"/>

0

试试这个:

<Button
    android:id="@+id/Button"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_gravity="center"
    android:layout_marginBottom="6px"
    android:layout_marginRight="3px"
    android:layout_toRightOf="@+id/Logo"
    android:scaleType="fitStart"
    android:background="@drawable/ImageYouWantToShow" />

Button imgButton= (Button) this.findViewById(R.id.Button)

0

尝试使用这个替代按钮

<FrameLayout
    android:layout_width="40dp"
    android:layout_height="100dp"
    >
    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/prevButton"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:paddingTop="16dp"
            android:paddingBottom="16dp"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:src="@drawable/arrow_left"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:background="#00000000"
            android:enabled="false"
            android:clickable="false"
            />
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="prev"
            android:textSize="20sp"
            android:gravity="center"
            android:background="#00000000"
            android:textColor="#FF000000"
            android:enabled="false"
            android:clickable="false"
            android:layout_weight="1"
            />

    </LinearLayout>

</FrameLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接