135

我有一个ImageButton,我想在上面显示一个文本和一个图像。但是当我尝试模拟器时:

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

我得到图像但没有文字。如何显示文本?请帮我!

4

12 回答 12

292

由于您不能使用android:text,我建议您使用普通按钮并使用其中一个复合可绘制对象。例如:

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

您可以使用以下命令将可绘制对象放在您想要的任何位置:drawableTopdrawableBottom或。drawableLeftdrawableRight

更新

对于一个按钮,这也很好用。放android:background就好了!

<Button
    android:id="@+id/fragment_left_menu_login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/button_bg"
    android:text="@string/login_string" />

我刚遇到这个问题并且工作正常。

于 2010-12-16T02:55:44.220 回答
68

ImageButton如果您真的想这样做,从技术上讲,可以在上面加上标题。只需TextViewImageButtonusing上放一个FrameLayout. 请记住不要使Textview可点击。

例子:

<FrameLayout>
    <ImageButton
        android:id="@+id/button_x"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@null"
        android:scaleType="fitXY"
        android:src="@drawable/button_graphic" >
    </ImageButton>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:clickable="false"
        android:text="TEST TEST" >
    </TextView>
</FrameLayout>
于 2011-07-19T16:30:33.520 回答
10

伙计们,我需要开发设置和注销按钮,我使用了下面的代码。 在此处输入图像描述

    <Button
        android:id="@+id/imageViewLogout"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/size_30dp"
        android:layout_alignParentLeft="true"
        android:text="Settings"
        android:drawablePadding="10dp"
        android:background="@android:color/transparent"
        android:layout_alignParentBottom="true"
        android:drawableTop="@drawable/logout" />
于 2017-07-05T10:00:38.930 回答
4

实际上,android:text不是接受的参数,ImageButton 但是,如果您尝试获取具有指定背景(不是 android 默认值)的按钮,请使用android:backgroundxml 属性,或者从类中声明它.setBackground();

于 2010-12-16T02:52:45.530 回答
4

您可以使用 aLinearLayout而不是使用Button它,这是我在我的应用程序中使用的一种安排

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:background="@color/mainColor"
        android:orientation="horizontal"
        android:padding="10dp">

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/ic_cv"
            android:textColor="@color/offBack"
            android:textSize="20dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="@string/cartyCv"
            android:textColor="@color/offBack"
            android:textSize="25dp" />

    </LinearLayout>
于 2017-01-06T21:02:56.447 回答
3

您可以改用常规Button和 android:drawableTop 属性(或左、右、下)。

于 2010-12-16T02:56:47.067 回答
3

我通过将ImageButtonandTextView放在LinearLayout垂直方向的 a 中解决了这个问题。效果很好!

<LinearLayout
    android:id="@+id/linLayout"
    android:layout_width="80dp"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageButton
        android:id="@+id/camera_ibtn"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_gravity="center"
        android:background="@drawable/camera" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/take_pic"
        android:textColor="#FFFFFF"
        android:textStyle="bold" />

</LinearLayout>
于 2015-05-03T16:27:48.587 回答
2

最好的办法:

<Button 
android:text="OK" 
android:id="@+id/buttonok" 
android:background="@drawable/buttonok"
android:layout_width="match_parent" 
android:layout_height="wrap_content"/>
于 2012-10-18T12:25:12.400 回答
1

这是一个很好的圆圈示例:

drawable/circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid
        android:color="#ff87cefa"/>

    <size
        android:width="60dp"
        android:height="60dp"/>
</shape>

然后是 xml 文件中的按钮:

<Button
    android:id="@+id/btn_send"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:background="@drawable/circle"
    android:text="OK"/>
于 2015-05-10T16:35:33.860 回答
1

这是解决方案

<LinearLayout
    android:id="@+id/buttons_line1"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ImageButton
        android:id="@+id/btn_mute"
        android:src="@drawable/btn_mute"
        android:background="@drawable/circle_gray"
        android:layout_width="60dp"
        android:layout_height="60dp"/>
    <ImageButton
        android:id="@+id/btn_keypad"
        android:layout_marginLeft="50dp"
        android:src="@drawable/btn_dialpad"
        android:background="@drawable/circle_gray"
        android:layout_width="60dp"
        android:layout_height="60dp"/>
    <ImageButton
        android:id="@+id/btn_speaker"
        android:layout_marginLeft="50dp"
        android:src="@drawable/btn_speaker"
        android:background="@drawable/circle_gray"
        android:layout_width="60dp"
        android:layout_height="60dp"/>
</LinearLayout>
<LinearLayout
    android:layout_below="@+id/buttons_line1"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_marginTop="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:text="mute"
        android:clickable="false"
        android:textAlignment="center"
        android:textColor="@color/Grey"
        android:layout_width="60dp"
        android:layout_height="wrap_content"/>
    <TextView
        android:text="keypad"
        android:clickable="false"
        android:layout_marginLeft="50dp"
        android:textAlignment="center"
        android:textColor="@color/Grey"
        android:layout_width="60dp"
        android:layout_height="wrap_content"/>
    <TextView
        android:text="speaker"
        android:clickable="false"
        android:layout_marginLeft="50dp"
        android:textAlignment="center"
        android:textColor="@color/Grey"
        android:layout_width="60dp"
        android:layout_height="wrap_content"/>
</LinearLayout>
于 2020-09-22T02:07:42.760 回答
1

最佳展示方式Textbutton带图片)

带有图像背景的按钮上的文本示例

您的问题:如何text显示imagebutton

:你不能text用显示imageButton在接受的答案中告诉的方法也不起作用。

因为

如果你使用android:drawableLeft="@drawable/buttonok"那么你不能设置drawablecenter.button

如果你使用android:background="@drawable/button_bg"那么color你的drawable将被改变。

在 android 世界中,有数以千计的选项可以做到这一点。但是根据我的观点,我在这里提供了最好的替代方案。(见下文)

解决方案:cardView使用LinearLayout

drawable/image使用 inLinearLayout因为它显示在中心。在您的帮助下,textView您可以text对此进行设置。我们制作cardView背景transparent

<androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="99dp"
                android:layout_margin="16dp"
                app:cardBackgroundColor="@android:color/transparent"
                app:cardElevation="0dp"
                app:cardUseCompatPadding="true">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/your_selected_image"
                    >

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:text="Happy Coding"
                        android:textSize="33sp"
                        android:gravity="center"
                        >
                    </TextView>

                </LinearLayout>
            </androidx.cardview.widget.CardView>

这里我解释一些术语:

app:cardBackgroundColor="@android:color/transparent"用于制作透明背景cardView

app:cardElevation="0dp"用于隐藏周围的海拔线cardView

app:cardUseCompatPadding="true"它提供的实际大小为cardView. 使用时始终使用它cardView

将您设置image/drawableLinearLayout背景。

对不起,我的英语不好。

快乐编码:)

于 2020-12-06T19:20:29.977 回答
0

ImageButton不能有text(或者,至少,android:text没有在它的属性中列出)。

诀窍是:

看起来您需要使用Button(并查看drawableTopor setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)).

于 2014-12-22T12:09:11.997 回答