Set the text in ImageButton

时光怂恿深爱的人放手 提交于 2019-11-30 04:05:07

问题


I have a ImageButton which looks like this.

 <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);

Now I need to add a dynamic text in ImageButton programmatically and I cannot use button. How can I do it?

Thanks In Advance ...


回答1:


ImageButton cannot contain text:

Options you can try

1) use Button instead of ImageButton

2) use ImageButton and TextView below to show text

Hope this could help




回答2:


You cannot set text to ImageButton because it has no method as setText() or android:text property.

ImageButtons can't have text (or, at least, android:text isn't listed in its attributes). It looks like you need to use Button (and look at drawableTop or setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)).

Instead of ImageButton try with Button. You can able to add the button background image.So try with Button instead of ImageButton.




回答3:


you use of drawableRight or drawableLeft attribute and use text attribute for create Button with text and image.

<Button 
 android:id="@+id/buttonok" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content"
 android:drawableLeft="@drawable/buttonok"
 android:text="OK"/>



回答4:


Try this instead of Button

<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>



回答5:


Try this:

<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)


来源:https://stackoverflow.com/questions/21776676/set-the-text-in-imagebutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!