安卓开发中,很多人都会遇到一个问题:如何让按钮带上图片以及文字描述呢?这里我找了几种解决方案:
一、布局文件直接使用Button,使用自带的drawableTop设置图片相对文本位置,然后设置文本即可
<Button
android:id="@+id/f1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
<span style="color:#ff0000;">android:drawableTop="@drawable/food1"</span>
android:text="1" />
foodBtn1 = (Button) this.findViewById(R.id.f1);
foodBtn1.setText("鱼香肉丝");
下图为按钮效果(当然也可以使用drawableBottom,drawableLeft,drawableRight来设置图片文字相对不同的位置)
二、布局文件使用ImageButton,自定义一个MyImageButton,重写其中的绘制按钮的方法(不得不吐槽。。。ImageButton竟然不能直接加上文字还不如Button呢)
public class MyImageButton extends ImageButton {
private String text = null;
private int color;
public MyImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setText(String text){
this.text = text;
}
public void setColor(int color){
this.color = color;
}
<span style="color:#ff0000;">protected void onDraw(Canvas canvas)</span>{
super.onDraw(canvas);
Paint paint = new Paint();
paint.setTextAlign(Paint.Align.CENTER);
paint.setColor(color);
canvas.drawText(text, 15, 20, paint);
}
}
然后在布局文件中引用定义好的MyImageButton
<com.nju.rxb.component.MyImageButton
android:id="@+id/loginBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<span style="color:#ff0000;">android:src="<span style="font-family: Arial, Helvetica, sans-serif;">@drawable/loginBtnImg</span></span><span style="font-family: Arial, Helvetica, sans-serif;">"/></span>
最后需要在activity代码中设置按钮需要显示的文字以及颜色
<span style="font-family: Arial, Helvetica, sans-serif;">loginBtn = (MyImageButton) this.findViewById(R.id.loginBtn);</span>
<span style="font-family: Arial, Helvetica, sans-serif;">loginBtn.setText("登录");</span>
<span style="font-family: Arial, Helvetica, sans-serif;">loginBtn.setColor(Color.WHITE);</span>
此方法较为常用,能够自行定义文字的大小样式位置,只需在onDraw()方法中实现即可
三、将一个ImageButton和一个TextView封装在一个LinearLayout中,对整个LinearLayout进行点击监听(即整个LinearLayout为一个按钮)
个人认为最麻烦的一个方法。。。主要是因为我压根没想过这也能实现!!!╮(╯▽╰)╭
public class ImageButton_layout extends LinearLayout{
<span style="white-space:pre"> </span>private ImageView imageViewbutton;
private TextView textView;
public ImageButton_define(Context context,AttributeSet attrs) {
super(context,attrs);
imageViewbutton = new ImageView(context, attrs);
imageViewbutton.setPadding(0, 0, 0, 0);
textView =new TextView(context, attrs);
//设置文字水平居中
textView.setGravity(android.view.Gravity.CENTER_HORIZONTAL);
textView.setPadding(0, 0, 0, 0);
//进行点击监听
setClickable(true);
setFocusable(true);
setBackgroundResource(android.R.drawable.btn_default);
//垂直布局
setOrientation(LinearLayout.VERTICAL);
addView(imageViewbutton);
addView(textView);
}
}
然后也是在布局文件中引用该组件(布局?)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.test.ImageButton_layout
android:id="@+id/loginBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/loginImg"
android:background="#00000000"
android:text="登录"
android:textColor="#cc0000"
>
</com.test.ImageButton_layout>
</LinearLayout>
最后定义activity运行。。。效果自行查看,本人实在懒得截图了