1
我有以下的图像按钮的活动:为什么Android Activity在更改背景后移动按钮?
<ImageButton android:id="@+id/nextPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/nextPageString"
android:alignParentRight="True"
android:alignParentBottom="True"
android:onClick="NextPage" />
我的按钮出现在我所期望的屏幕的右下角。
当我点击按钮,更改与下面的代码背景:
public void NextPage(View view){
Resources res = this.getResources();
Drawable d = res.getDrawable(R.drawable.nextBackgroundImage);
view.setBackground(d);
}
我点击按钮,它会显示我的新的背景正是我要在第一时间以后,然而,它移动的图像按钮到屏幕的中心。不知道为什么。我希望它留在右下角。
我猜'nextBackgroundImage'与你的ImageButton不一样大小。这就是为什么它保持图像的宽高比。并显示在中心。 –
尝试将android:layout_width和android:layout_height设置为测试的某些定义值 –
图像有多大?虽然这不会解决你的问题,你可以使用'view.setBackgroundResource(R.drawable.nextBackgroundImage);'和上面的代码设置宽度两次,但不是高度... – Sam