简体   繁体   中英

Android- put button on top of view

I am creating an app where the user can draw, and then change the "settings" of how they are drawing. In the DrawingActivity class, onCreate, I setContentView as a class I created, called DrawingView that extends View. I would like to place a button on top of DrawingView, but I cannot figure out how. Here is the DrawingActivity class.

public class DrawingActivity extends Activity {

DrawingView dv ;   

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    dv = new DrawingView(this);
    setContentView(dv);

}

RelativeLayout allows putting views on top of each other. You could add your DrawingView to the layout with android:layout_width="match_parent" and android:layout_height="match_parent" , and then add your button with appropriate positioning attributes. That is one way of achieving what you want, I am sure there are others.

If you want to put an icon instead of a button on the ActionBar I recommend you check and download the ActionBarSherlock library to avoid compatibility problems and to make your work easy. For instance, if you have download and installed SherlockActionBar, you should follow nexts steps:

  1. Extend your activity from SherlockActivity instead of Activity:

     public class DrawingActivity extends SherlockActivity {...your code} 

Replace the "ic_action_trash" by your icon on res/drawable directories (drawable-hdpi, drawable-ldpi, ...)

  1. create a xml file on res/menu folder, called menu_main.xml (for example)

在此处输入图片说明

  1. Override the methods onCreateOptionsMenu and onOptionsItemSelected below your OnCreate method in the DrawingActivity class like this:

     @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.menu_main, menu); } 

    \n\n
     @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.delete_events) deleteEvent(); return true; } 
    \n\n

And here is the result: 在此处输入图片说明 However, to check your requirements, I think you should change the methods above like this:

And here is the result: 在此处输入图片说明 However, to check your requirements, I think you should change the methods above like this:

And here is the result: 在此处输入图片说明 However, to check your requirements, I think you should change the methods above like this:

 @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { //inflater.inflate(R.menu.menu_main, menu); SubMenu submenu = menu.addSubMenu(""); submenu.setIcon( R.drawable.abs__ic_menu_moreoverflow_normal_holo_dark); submenu.add(1, 1, 1, R.string.add); submenu.add(1, 2, 2, R.string.delete); submenu.add(1, 3, 3, R.string.about); submenu.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case 1: addEvent(); break; case 2: deleteEvent(); break; case 3: about(); break; default: return true; } return true; } 

When you click on the top icon you would see this: 在此处输入图片说明

Hope this works for you!

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