Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将图像浮动到Android按钮的右侧?_Android_Android Studio - Fatal编程技术网

如何将图像浮动到Android按钮的右侧?

如何将图像浮动到Android按钮的右侧?,android,android-studio,Android,Android Studio,我正在尝试设置在按钮最右侧有图标的按钮。 到目前为止,我得到了以下信息: <style name="MenuButton" parent="@android:style/Widget.Holo.Light.Button"> <item name="android:textColor">#000000</item> <item name="android:layout_margin">0dp</item> <

我正在尝试设置在按钮最右侧有图标的按钮。 到目前为止,我得到了以下信息:

<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>

#000000
0dp
60dp
填补家长的空缺
10dp
大胆的
@可绘制/箭头图标
170dip
@可绘图/自定义菜单\u btn\u bg
然而,这只会将图像放在按钮内文本的右侧,但我需要它位于按钮的最右侧,无论按钮中有多少文本

到目前为止,我只是使用drawablePadding将其向右推,但这不会起作用,因为我将动态创建这些按钮,并且不知道需要多少填充

谢谢

试试这个:

 <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"
   />

您可以扩展
按钮
类并自己绘制图像。通常我会避免使用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);
        }
    }
}