如何将text
添加到ImageButton
?
在我的应用程序中,我想在我的三个text
中添加ImageButtons
,让用户知道每个游戏打开的内容。
E.g。将“数学游戏”添加到下面屏幕截图中显示的第一个循环ImageButton
:
XML代码:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<com.mikhaellopez.circularimageview.CircularImageView
android:id="@+id/ibFirstTabMaths"
android:layout_width="250dp"
android:layout_height="250dp"
android:src="@drawable/mathsicon"
app:border="true"
app:border_color="@color/GrayLight"
app:border_width="4dp"
app:shadow="true" />
<com.mikhaellopez.circularimageview.CircularImageView
android:id="@+id/ibFirstTabMemory"
android:layout_width="250dp"
android:layout_height="250dp"
android:src="@drawable/memoryicontwo"
app:border="true"
app:border_color="@color/GrayLight"
app:border_width="4dp"
app:shadow="true" />
<com.mikhaellopez.circularimageview.CircularImageView
android:id="@+id/ibFirstTabStroop"
android:layout_width="250dp"
android:layout_height="250dp"
android:src="@drawable/stroopicon"
app:border="true"
app:border_color="@color/GrayLight"
app:border_width="4dp"
app:shadow="true" />
</LinearLayout>
</ScrollView>
活动代码:
public class firstActivity extends Activity implements View.OnClickListener {
// Declare vars
CircularImageView ibMaths;
CircularImageView ibMemory;
CircularImageView ibStroop;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.first_tab);
initialiseVars();
}
public void initialiseVars() {
ibMaths = (CircularImageView) findViewById(R.id.ibFirstTabMaths);
ibMemory = (CircularImageView) findViewById(R.id.ibFirstTabMemory);
ibStroop = (CircularImageView) findViewById(R.id.ibFirstTabStroop);
// set on click listeners for the buttons
ibMaths.setOnClickListener(this);
ibMemory.setOnClickListener(this);
ibStroop.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.ibFirstTabMaths:
Intent openMathsGame = new Intent("com.example.brianapp.meditation");
// Start activity
startActivity(openMathsGame);
break;
case R.id.ibFirstTabMemory:
Intent openMemGame = new Intent("com.example.brianapp.MemoryGame");
// Start activity
startActivity(openMemGame);
break;
case R.id.ibFirstTabStroop:
// change this to make sure it opens the med screen
Intent openStroopGame = new Intent("com.example.brianapp.Stroop");
// Start activity
startActivity(openStroopGame);
break;
}// switch end
}
}
答案 0 :(得分:0)
一个选项是使用Text创建图像并将其用作Imagebutton的源并将背景提供为null。例如
android:background="@null"
android:src="@drawable/image_with_text"
答案 1 :(得分:0)
您正在为ImageView创建自定义视图,因此您可以使用画布来执行此操作 在draw方法中,在视图中心获取canvas和drawtext。
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>
attr.xml
<?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>
ImgBtn.class
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);
}
}
此处完整代码;)