Android 在 ImageButton 中设置文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21776676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:03:04  来源:igfitidea点击:

Set the text in ImageButton

androidimagebutton

提问by Suniel

I have a ImageButton which looks like this.

我有一个像这样的 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);

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

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

Thanks In Advance ...

提前致谢 ...

回答by nitesh

ImageButtoncannot contain text:

ImageButton不能包含文本:

Options you can try

您可以尝试的选项

1) use Buttoninstead of ImageButton

1) 使用Button代替ImageButton

2) use ImageButtonand TextViewbelow to show text

2) 使用ImageButtonand TextViewbelow 来显示文本

Hope this could help

希望这可以帮助

回答by Jebasuthan

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

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

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

ImageButtons 不能有文本(或者,至少,android:text它的属性中没有列出)。看起来您需要使用 Button(并查看drawableTopsetCompoundDrawablesWithIntrinsicBounds(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.

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

回答by Digicom

you use of drawableRightor drawableLeftattribute and use text attribute for create Button with text and image.

您使用drawableRightdrawableLeft属性并使用 text 属性创建带有文本和图像的按钮。

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

回答by endo

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>

回答by Kashif Ks

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)