Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.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
Java 为什么Android活动在更改背景后会移动按钮?_Java_Android - Fatal编程技术网

Java 为什么Android活动在更改背景后会移动按钮?

Java 为什么Android活动在更改背景后会移动按钮?,java,android,Java,Android,我有一个带有以下图像按钮的活动: <ImageButton android:id="@+id/nextPage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/nextPageString" android:alignParentRight="True" androi

我有一个带有以下图像按钮的活动:

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

在我第一次单击按钮后,它会完全按照我的需要显示我的新背景,但是,它会将图像按钮移动到屏幕的中心。不知道为什么。我希望它保持在右下角。

正如这里的一些评论所指出的,背景图像的大小可能是按钮“移动”的原因。当
layout\u width
layout\u height
设置为wrap时,如果视图的内容(包括其背景)发生变化,大小将发生变化


+1对于Alex,试着在那里设置一个特定的尺寸来测试,看看会发生什么(例如,200dp宽x 80dp高)

正如这里的一些评论所指出的,背景图像的大小可能是按钮“移动”的原因。当
layout\u width
layout\u height
设置为wrap时,如果视图内容(包括其背景)发生变化,则大小将发生变化


+1对于Alex,试着在那里设置一个特定的尺寸来测试,看看会发生什么(例如,200dp宽x 80dp高)

我猜
nextBackgroundImage
与您的
ImageButton
大小不一样。这就是为什么它保持图像的纵横比。并将其显示在中间。尝试将android:layout\u width和android:layout\u height设置为一些定义的值,以测试图像有多大?虽然这不能解决您的问题,但您可以使用
view.setBackgroundResource(R.drawable.nextBackgroundImage)nextBackgroundImage
与您的
ImageButton
大小不一样。这就是为什么它保持图像的纵横比。并将其显示在中间。尝试将android:layout\u width和android:layout\u height设置为一些定义的值,以测试图像有多大?虽然这不能解决您的问题,但您可以使用
view.setBackgroundResource(R.drawable.nextBackgroundImage)public void NextPage(View view){
    Resources res = this.getResources();
    Drawable d = res.getDrawable(R.drawable.nextBackgroundImage);
    view.setBackground(d);
}