Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓:向图像按钮添加文本?_Android_Android Layout_Imagebutton - Fatal编程技术网

Android 安卓:向图像按钮添加文本?

Android 安卓:向图像按钮添加文本?,android,android-layout,imagebutton,Android,Android Layout,Imagebutton,如何向ImageButton添加文本 在我的应用程序中,我想向三个ImageButton中的每一个添加文本,让用户知道每个按钮打开的游戏 例如,将数学游戏添加到第一个圆形图像按钮,如下面的屏幕截图所示: XML代码: 一个选项是使用文本创建图像,并将其用作Imagebutton的源,并将背景设置为null。比如说 android:background="@null" android:src="@drawable/image_with_text" 您正在为ImageView创建自

如何向ImageButton添加文本

在我的应用程序中,我想向三个ImageButton中的每一个添加文本,让用户知道每个按钮打开的游戏

例如,将数学游戏添加到第一个圆形图像按钮,如下面的屏幕截图所示:

XML代码:


一个选项是使用文本创建图像,并将其用作Imagebutton的源,并将背景设置为null。比如说

    android:background="@null"
    android:src="@drawable/image_with_text"

您正在为ImageView创建自定义视图,以便可以使用canvas执行此操作。 在绘图方法上,在视图中心获取画布和绘图文本

main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/packageName"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

 <packageName.ImgBtn
    android:id="@+id/imgBtn1"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    app:text="your text here or in String.xml as reference"
    android:src="@drawable/bookmark" />

</RelativeLayout>  

完整的代码在这里

您始终可以制作一个相对视图,并在其中放置您的CircularImageView,占据所有布局。接下来,您只需在同一个RelativeLayout中放置3或4个文本视图。然后,你只需要做一个OnClickListener到相对的Yout。谢谢,一定有一个更简单的方法可以做到这一点?如果你想给出文本,为什么不使用按钮呢?第二,你仍然想要文本到图像按钮,然后你可以创建该文本的图像,并将该图像设置为该图像按钮:你可以将textView放在每个图像按钮下,或者像Android那样对我来说就是一切。谢谢,这会使图像也保留在按钮内吗?这听起来像我需要做的,你能不能贴一个代码的例子,这样我就可以接受答案。谢谢这是一个非常糟糕的实现。文本位于图像内部,大小约为3dp,无法读取。此外,在将“我的工具栏”中的所有图像按钮转换为自定义图像按钮后,工具栏最终接管了整个视图。
    android:background="@null"
    android:src="@drawable/image_with_text"
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/packageName"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

 <packageName.ImgBtn
    android:id="@+id/imgBtn1"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    app:text="your text here or in String.xml as reference"
    android:src="@drawable/bookmark" />

</RelativeLayout>  
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomWidgetAttr" >
       <attr name="text" format="string|reference"/>
       <attr name="pic" format="reference"/>
    </declare-styleable>
</resources>  
public class ImgBtn extends ImageButton {
private String Text;
private Drawable Logo;
private Paint paint;

public ImgBtn(Context context, AttributeSet attrs) {
    super(context, attrs);

    TypedArray typedArray = getContext().obtainStyledAttributes(attrs,
            R.styleable.CustomWidgetAttr);

    Text = typedArray.getString(R.styleable.CustomWidgetAttr_text);
    paint = new Paint();
    paint.setFlags(Paint.ANTI_ALIAS_FLAG);
    paint.setTextAlign(Align.CENTER);
    paint.setColor(Color.BLACK);
    paint.setStyle(Style.FILL);

}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // TODO Auto-generated method stub
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
}

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);

    int w = this.getMeasuredWidth();
    int h = this.getMeasuredHeight();

    canvas.drawText(Text, w/2, h/2, paint);

}

}