简体   繁体   English

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

[英]Why does Android Activity move button after changing background?

I have an Activity with the following image button: 我有一个带有以下图像按钮的活动:

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

My Button appears in the bottom right corner of the screen as I would expect. 我的按钮出现在屏幕的右下角。

When I click the button I change the background with the following code: 当我单击按钮时,使用以下代码更改背景:

public void NextPage(View view){
    Resources res = this.getResources();
    Drawable d = res.getDrawable(R.drawable.nextBackgroundImage);
    view.setBackground(d);
}

After I click the button the first time it displays my new background exactly as I want, however, it moves the image button to the center of the screen. 第一次单击按钮后,它将完全按照我的意愿显示我的新背景,但是,它将图像按钮移到屏幕中央。 Not sure why. 不知道为什么。 I would like it to stay in the bottom right corner. 我希望它保留在右下角。

As several of the comments here have noted, the SIZE of your background image is likely why you're button is "moving." 正如这里的几条评论所指出的,您的按钮背景“移动”的原因可能是背景图像的大小。 With layout_width and layout_height set to wrap, the size will change if the content of the view (including its background) changes. layout_widthlayout_height设置为wrap,如果视图的内容(包括其背景)发生更改,则大小将更改。

+1 for Alex, try setting a specific size there just to TEST and see what happens (200dp width x 80dp height, for example). 为+1设置Alex,尝试在此处为TEST设置一个特定的大小,然后看看会发生什么(例如200dp宽度x 80dp高度)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM