开发者

How to add button/menu item in android

I m a novice in android.. i want to add a button or menu item in message inbox or ema开发者_如何学运维il. how can i do so.


i want to add a button or menu item in message inbox or email

If you mean you want to add a button or menu item to somebody else's application (Email, Gmail, Messaging, etc.), you can't -- sorry!


Well this is a very broad question because there are many ways to add a button, and it really just depends on where you would like it to appear on the screen, and when. But suppose you wanted a button at the bottom of the screen that was always there, you might do something like this:

In your layout xml you will do something like this:

    ?xml...
    <LinearLayout ...
       android:layout_height="fill_parent"
       android:layout_width="fill_parent" 
       android:orientation="vertical" >

       .... other layout items (lists, images, ext) ....

       <LinearLayout
          android:layout_height="wrap_content"
          android:layout_width="fill_parent"
          android:orienation="horizontal" >

          <Button
             android:layout_height="wrap_content" <!--the button is only as tall as it needs to be -->
             android:layout_width="fill_parent" 
             android:layout_weight="1"            <!-- when width is "fill parent" and weight is "1" the element will share equal space with other elements with weight "1" -->
             android:text="Ok"
             android:id="@-id/ok_button" ></Button>
          <Button
             android:layout_height="wrap_content" 
             android:layout_width="fill_parent" 
             android:layout_weight="1"            
             android:text="Cancel"
             android:id="@-id/cancel_button" ></Button>

       </LinearLayout>
    </LinearLayout>

Then, in onCreate() in your activity, you will need to make objects associated with the buttons, and define their behavior

Button okButton = (Button) findViewById(R.id.ok_button);
Button cancelButton = (Button) findViewById(R.id.cancel_button;

okButton.setOnClickListener(new onClickListener() {
   public void onClick()
   {
      //do something
   }
});
cancelButton.setOnClickListener(new onClickListener() {
   public void onClick()
   {
      //do something
   }
});

I hope this helps you out

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜