简体   繁体   中英

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.

+1 for Alex, try setting a specific size there just to TEST and see what happens (200dp width x 80dp height, for example).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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