Android 如何在ImageButton上显示文本?
我有一个Android 如何在ImageButton上显示文本?,android,imagebutton,Android,Imagebutton,我有一个ImageButton,我想在上面显示一个文本和一个图像。但当我在emulator上尝试时: <ImageButton android:text="OK" android:id="@+id/buttonok" android:src="@drawable/buttonok" android:layout_width="match_parent" android:layout_height="wrap_content" />
ImageButton
,我想在上面显示一个文本和一个图像。但当我在emulator上尝试时:
<ImageButton
android:text="OK"
android:id="@+id/buttonok"
android:src="@drawable/buttonok"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
我得到了图像,但没有文本。如何显示文本?请帮帮我 实际上,
android:text
不是ImageButton
但是,如果您试图获取具有指定背景(非android默认)的按钮,请使用
android:background
xml属性,或者使用.setBackground()从类中声明它代码>由于您不能使用android:text
我建议您使用普通按钮并使用一个复合绘图工具。例如:
<Button
android:id="@+id/buttonok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/buttonok"
android:text="OK"/>
我刚刚遇到了这个问题,现在工作得很好。你可以使用常规的
按钮和android:drawableTop属性(或左、右、下)。如果你真的想在图像按钮上添加标题,技术上是可行的。只需使用FrameLayout
在ImageButton
上放置一个TextView
。记住不要让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>
最佳方式:
<Button
android:text="OK"
android:id="@+id/buttonok"
android:background="@drawable/buttonok"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
ImageButton
不能有文本(或者至少,android:text
未在其属性中列出)
诀窍是:
看起来您需要使用按钮(并查看drawableTop
或SetCompoundDrawableSwithintInInsitCBounds(int,int,int,int))
我通过将图像按钮和文本视图
放在垂直方向的线性布局中解决了这个问题。很好
<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>
这里有一个很好的圆示例:
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"/>
您可以使用线性布局
而不是使用按钮
这是我在应用程序中使用的一种布局
<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>
伙计们,我需要开发设置和注销按钮,我使用了下面的代码。
在按钮上显示文本的最佳方式(带图像)
您的问题:如何在图像按钮
上显示文本
回答:不能使用图像按钮
显示文本
。在公认答案中说出的方法也不起作用
因为
如果使用android:drawableLeft=“@drawable/buttonok”
,则无法在按钮的中心设置drawable
如果您使用android:background=“@drawable/button\u bg”
,则您的drawable
的颜色将发生更改
在android世界中,有成千上万的选项可以这样做。但根据我的观点,这里我提供了最好的替代方案。(见下文)
解决方案:将cardwiew
与LinearLayout
您的可绘制/图像
用于线性布局
,因为它显示在中间。在textView
的帮助下,您可以在此设置text
。我们将cardwiew
背景设置为透明
<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”
用于制作cardwiew的透明背景
app:cardElevation=“0dp”
用于隐藏卡视图周围的水平线
app:cardUseCompatPadding=“true”
its提供了cardwiew
的实际大小。使用cardView
将线性布局中的图像/可绘制
设置为背景
对不起,我英语不好
快乐编码:)此选项效果最好,除非您需要某种外观疯狂的按钮布局,例如由两个文本视图组成的图像。我最初尝试了一个定义为widget.button的布局对象,然后在其中放置了一个imageview和一个textview,但在对其进行分析之后,以及上面的按钮布局,我在FramLayout上节省了近30%的测量时间、50%的布局时间和15-20%的绘图时间,并从38个对象减少到了26个。这是一个相当可观的节约。@shim drawableTop将把drawable放在按钮的顶部,drawableRight放在底部,等等@Renanbandera是的,使用tsetCompoundDrawables*()
methodsHow我可以缩放图像以适应按钮吗?或者它会自动调整图像以与按钮匹配。与@Stallman的问题相同,如何将图像与按钮匹配,并将其设置为左侧?这似乎与OP最初尝试的没有任何不同。抱歉,我弄错了代码。was’t ImageButton,was just Button。这似乎对棒棒糖不起作用,但对其他的棒棒糖效果很好。这种方法的问题是文本不受按钮状态(禁用等)的影响。。。除非你是手动操作。很好的解决方案!onClick在线性布局上也能完美工作。看不出我们应该使用按钮或图像按钮的原因
<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" />
<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>
<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>