我已将我的按钮宽度设置为fill_parent
。如何在按钮的中心设置图片和文字
so是否可以设置按钮中心的图像和文本。
存在对drawable_Left
选项,但按钮的左边对齐图像...
我想是这样的。
.... [image] [text] .... |
感谢:使用android:drawableTop=""
的按钮,然后设置图像先设置背景,然后通过android:text=""
我已将我的按钮宽度设置为fill_parent
。如何在按钮的中心设置图片和文字
so是否可以设置按钮中心的图像和文本。
存在对drawable_Left
选项,但按钮的左边对齐图像...
我想是这样的。
.... [image] [text] .... |
感谢:使用android:drawableTop=""
的按钮,然后设置图像先设置背景,然后通过android:text=""
使用android:gravity="center_vertical"
试试这个gravity
至center
然后使用drawable_Left
。
先给设置文本:在你的布局
你,你应该使用吹代码让你的问题的解决相对布局把它放在你的XML文件
android:layout_centerHorizontal ="true"
<Button android:text="Button" android:id="@+id/button1"
android:layout_height="wrap_content" android:background="@drawable/icon"
android:layout_width="200dp" android:gravity="center"></Button>
希望它能帮助;
下面是一个效用函数中心内部左侧图像和文本Button
:
public static void centerImageAndTextInButton(Button button) {
Rect textBounds = new Rect();
//Get text bounds
CharSequence text = button.getText();
if (text != null && text.length() > 0) {
TextPaint textPaint = button.getPaint();
textPaint.getTextBounds(text.toString(), 0, text.length(), textBounds);
}
//Set left drawable bounds
Drawable leftDrawable = button.getCompoundDrawables()[0];
if (leftDrawable != null) {
Rect leftBounds = leftDrawable.copyBounds();
int width = button.getWidth() - (button.getPaddingLeft() + button.getPaddingRight());
int leftOffset = (width - (textBounds.width() + leftBounds.width()) - button.getCompoundDrawablePadding())/2 - button.getCompoundDrawablePadding();
leftBounds.offset(leftOffset, 0);
leftDrawable.setBounds(leftBounds);
}
}
它使用Button
的宽度进行计算,所以你必须与要调用此检查在正确的地方。也就是说,宽度应该不是零。例如,如果您想在onCreate
中使用它,请使用button.getViewTreeObserver().addOnGlobalLayoutListener(...call it onGlobalLayout...)
。
使用drawable_Left后,您可以设置填充 – Lavanya 2011-05-19 09:30:35