我正在尝试设置按钮最右侧有一个图标的按钮。 到目前为止我得到了以下内容:
<style name="MenuButton" parent="@android:style/Widget.Holo.Light.Button">
<item name="android:textColor">#000000</item>
<item name="android:layout_margin">0dp</item>
<item name="android:minHeight">60dp</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:padding">10dp</item>
<item name="android:textStyle">bold</item>
<item name="android:drawableRight">@drawable/arrow_icon</item>
<item name="android:drawablePadding">170dip</item>
<item name="android:background">@drawable/custom_menu_btn_bg</item>
</style>
然而,这仅将图像放在按钮内文本的右侧,但无论按钮中有多少文本,我都需要它位于按钮的最右侧。
到目前为止,我只是使用drawablePadding向右推,但这不会起作用,因为我将动态创建这些按钮,并且不知道我需要多少填充。
由于
答案 0 :(得分:0)
试试这个:
<Button
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_margin="0dp"
android:drawableRight="@drawable/ic_launcher"
android:minHeight="60dp"
android:padding="10dp"
/>
答案 1 :(得分:0)
您可以扩展Button
课程并自己绘制图像。通常我会避免使用android:drawableRight
并定义自定义XML属性,但为了简单起见,我将“窃取”您提供的drawableRight。
public class MyButton extends Button {
private Drawable rightDrawable;
private Rect bounds;
/* do this for the other 2 constructors as well */
public MyButton(Context context) {
super(context);
init();
}
private void init() {
bounds = new Rect();
Drawable[] drawables = getCompoundDrawables();
rightDrawable = drawables[2];
if (rightDrawable != null) {
// we are going to draw this drawable, so remove it from the superclass'
// drawables
setCompoundDrawables(drawables[0], drawables[1], null, drawables[3]);
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (w != oldW || h != oldh) {
/*
* View's size changed, recalculate drawable bounds
*
* Do some math here to figure out where exactly the drawable should
* be drawn. Make sure to take into account the drawable's intrinsic
* height (use getIntrinsicHeight()), as well as the top, bottom, and
* right padding of this button (use getPaddingLeft(), etc.)
*
* For the sake of not leading you astray with code that may not work,
* I've not attempted those calculations. Eventually, you would call...
*/
rect.setBounds(left, top, right, bottom);
}
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (rightDrawable != null) {
rightDrawable.setBounds(rect);
rightDrawable.draw(canvas);
}
}
}