A. android中button上设置图片
android中button上设置图片的方法为:
1、自定义MyButton类
public class MyButton extends Button {
//This constructormust be
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyButton(Context context) {
super(context);
}
private Paint mPaint = null;
private String mText;
private int mX, mY;
public void onSetText(String text, int nLeft, int nBottom, int nTextSize,
int nTextColor) {
mPaint = new Paint();
mPaint.setTextSize(nTextSize);
mPaint.setColor(nTextColor);
this.mText = text;
this.mX = nLeft;
this.mY = nBottom;
}
private int mDownBmpId, mUpBmpId;
public void onSetBmp(int nDownID, int nUpID) {
this.mDownBmpId = nDownID;
this.mUpBmpId = nUpID;
}
@Override
public void onDraw(Canvas canvas) {
if (mPaint != null)
canvas.drawText(mText, mX, mY, mPaint);
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
super.setBackgroundResource(mDownBmpId);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
super.setBackgroundResource(mUpBmpId);
}
return super.onTouchEvent(event);
}
}
2、 在xml布局文件中添加MyButton控件,像应用普通的Button控件一样。
<com.MyButton
android:id="@+id/test_btn" android:layout_width="120px"
android:layout_height="fill_parent" android:text="Test"
android:background="@drawable/btn_u" />
其中com.MyButton是你定义的MyButton类所在的包名
3、在onCreate()中加载MyButton控件。
MyButton btn = (MyButton)findViewById(R.id.test_btn);
btn.onSetBmp(R.drawable.btn_d, R.drawable.btn_u);
其中btn_d表示为按下btn时背景图片,btn_u为默认状态下btn背景图片。
B. 怎么将Button上的文字和android:drawableLeft都居中
Button上的android:drawableLeft设置的图片就是居左,无法和文字一起居中,文字属性可以通过android:layout_gravity设置居中,想要android:drawableLeft设置的图片居中,可以通过另外的方法来实现,建议:两张图片合二为一,android:drawableLeft设置的图片直接和按钮背景图合到一张图片上,android:drawableLeft设置的图片大约位于按钮背景图三分之一处(黄金分割点),右侧空出文本区域。
C. Android Button设置.9格式图片当背景,发现Button的text不显示了
我觉得这个是你画的 .9 图的问题
右边跟下边有画成这样才能显示文字内容,至于为什么,请自行搜索一下.9图的用法
D. android圆形灰色图片做按钮背景,圆形背景上需要叠加一张图片和文字,文字要在图片的下方,怎么实现
用android:drawableTop设置图片就好了。
E. 怎么设置android button 里文字和drawableleft 居左
将图标和text在button的中间位置,现在是文字和图标距离很远
[专业]答案:Button上的android:drawableLeft设置的图片就是居左,无法和文字一起居中,文字属性可以通过android:layout_gravity设置居中,想要android:drawableLeft设置
F. android中button上的图案可以自定义吗
可以的。
package com.test;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.Button;
public class ImageTextButton2 extends Button {
private int resourceId = 0;
private Bitmap bitmap;
public ImageTextButton2(Context context) {
super(context,null);
}
public ImageTextButton2(Context context,AttributeSet attributeSet) {
super(context, attributeSet);
this.setClickable(true);
resourceId = R.drawable.icon;
bitmap = BitmapFactory.decodeResource(getResources(), resourceId);
}
public void setIcon(int resourceId)
{
this.bitmap = BitmapFactory.decodeResource(getResources(), resourceId);
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
// 图片顶部居中显示
int x = (this.getMeasuredWidth() - bitmap.getWidth())/2;
int y = 0;
canvas.drawBitmap(bitmap, x, y, null);
// 坐标需要转换,因为默认情况下Button中的文字居中显示
// 这里需要让文字在底部显示
canvas.translate(0,(this.getMeasuredHeight()/2) - (int) this.getTextSize());
super.onDraw(canvas);
}
}
G. android点击按钮之后改变按钮的文字
想在android点击按钮之后改变按钮的文字需要给按钮添加一个监听事件,然后一监听到该事件再给这个按钮调用setText()方法,在方法里可以给按钮设置文字,具体操作如下:
1、首先使用Android studio创建一个项目,如下图:
H. android 如何像win8图标一样在图片按钮上加文字
可以不使用button控件,而是用布局 ,在布局中定义文字大小位置,而布局大小就是你所谓的按钮大小其背景色就用此图片,最终给这个布局添加上onclick事件就可以了
I. android 点击ImagButton切换文字和图片用什么来实现
用xml来实现,再写一个效果描述的xml来指定正常状况下是什么图片,按下是什么图片等都可以
J. 如何在ImageButton上写字Android
因为ImageButton本身没有text属性,所以需要自定义一个新的ImageButton类,属于自定义View。安卓里所有的UI界面都是由View类和ViewGroup类及其子类组合而成的。ps:找美工在背景图片上加文字最方便。