In many cases, we need to customize the image button in android, instead of the system. Today, let's take a look at how to implement this simple requirement.
(1) first, prepare necessary resources. Here, I have prepared three images as follows:
(2) create an android project, create a drawable folder under res, and create a button. xml file under the drawable folder. the specific content of the file is as follows:
At the same time, copy the three images background.jpg?start_a.png=start_ B .png TO THE drawable-hdpi folder.
(3) Modify activity_main.xml in layout. The modified content is as follows:
Note that you need to set the background attribute of ImageButton to "#0000" so that the effect of the custom image button is not affected.
(4) The main Activity defaults to setContentView (R. layout. activity_main). The MainActivity content is as follows:
import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; }}
Right-click the project and run our android application. The result is as follows:
Source code:
Click an image button for tracking in android