gpt4 book ai didi

java - 为什么更改背景后 Android Activity 会移动按钮?

转载 作者:行者123 更新时间:2023-11-30 03:54:03 25 4
gpt4 key购买 nike

我有一个带有以下图像按钮的 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);
}

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

最佳答案

正如这里的一些评论所指出的,您的背景图片的大小可能是您的按钮“移动”的原因。当 layout_widthlayout_height 设置为 wrap 时,如果 View 的内容(包括其背景)发生变化,尺寸也会发生变化。

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

关于java - 为什么更改背景后 Android Activity 会移动按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13631833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com