如何在java中从xml更改ImageButton大小

时间:2015-04-03 16:27:43

标签: java android xml resize android-imagebutton

我的android项目有一些问题。

这是我的ImageButton的xml代码,具有固定的宽度和高度。

<ImageButton
    android:layout_width="120dp"
    android:layout_height="120dp"
    android:id="@+id/ball1"
    android:background="@drawable/ball"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

在这里,我在java中调用 ImageButton

ImageButton ball1 = (ImageButton) findViewById(R.id.ball1);

我的问题是,我需要做什么,减去我的 layout_width layout_height 在java代码中用10dp?

这样的事情:

int width = ball1.getWidth();
    int height = ball1.getHeight();
width = width - 10dp;
height = height - 10dp;

那么如何将高度和宽度减去10dp?

1 个答案:

答案 0 :(得分:0)

您可以通过

执行此操作
imagebutton.setLayoutParams(new LinearLayout.LayoutParams(50, 50));

或使用以下链接

http://android-coding.blogspot.in/2011/05/resize-button-programmatically-using.html

相关问题