简体   繁体   中英

View.setBackground() setting background of ImageButton?

I have an ImageButton:

<ImageButton 
android:id="@+id/nextPageB"
android:layout_width="50dp"
android:layout_width="50dp"
android:layout_alignParentBottom="True"
android:contentDescription="@string/nextPage"
android:onClick="NextPage"
android:src="@drawable/ic_launcher"
/>

This is part of a seperate activity that is started from my main activity:

Intent intent = new Intent(this, BookRead.class);
startActivity(intent);

Once in the new activity I call the NextPage method upon ImageButton click:

public void NextPage(View view)
{
    view.setBackgroud(res.getDrawable(R.Drawable.MyNewImage));
}

'res' is defined in my new activity in the onCreate method:

res = this.getResources();

For some reason when I click my ImageButton it changes the background on the ImageButton itself instead of the activity layout.

An imagebutton is also a view, and you are probably passing the imagebutton view to the NextPage method.

You could either get the correct view by referencing it from XML or do something like view.getParent().

onClick方法接收实际单击的视图。

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