2012-11-29 34 views
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); 
} 

我点击按钮,它会显示我的新的背景正是我要在第一时间以后,然而,它移动的图像按钮到屏幕的中心。不知道为什么。我希望它留在右下角。

+1

我猜'nextBackgroundImage'与你的ImageButton不一样大小。这就是为什么它保持图像的宽高比。并显示在中心。 –

+2

尝试将android:layout_width和android:layout_height设置为测试的某些定义值 –

+0

图像有多大?虽然这不会解决你的问题,你可以使用'view.setBackgroundResource(R.drawable.nextBackgroundImage);'和上面的代码设置宽度两次,但不是高度... – Sam

回答

1

由于这里的一些评论已经指出,背景图片的大小可能就是为什么你的按钮是“移动”。将layout_widthlayout_height设置为换行,如果视图的内容(包括其背景)发生更改,大小将会更改。

对Alex的+1,请尝试在那里设置一个特定的大小,然后查看会发生什么情况(例如200dp宽×80dp高)。