在ImageButton中设置文本

Sun*_*iel 7 android imagebutton

我有一个看起来像这样的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);
Run Code Online (Sandbox Code Playgroud)

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

提前致谢 ...

nit*_*esh 13

ImageButton 不能包含文字:

您可以尝试的选项

1)使用Button而不是ImageButton

2)使用ImageButtonTextView以下显示文字

希望这可以帮助


Jeb*_*han 5

您不能将文本设置为ImageButton,因为它没有setText()或android:text属性的方法.

ImageButtons不能包含文本(或者至少android:text没有在其属性中列出).看起来你需要使用Button(并查看drawableTopsetCompoundDrawablesWithIntrinsicBounds(int,int,int,int)).

而不是ImageButton尝试使用Button.您可以添加按钮背景图像.尝试使用Button而不是ImageButton.