如何在android中添加按钮/菜单项

时间:2010-06-06 14:30:46

标签: android

我是Android的新手..我想在消息收件箱或电子邮件中添加按钮或菜单项。我怎么能这样做。

2 个答案:

答案 0 :(得分:1)

  

我想添加一个按钮或菜单项   消息收件箱或电子邮件

如果您的意思是想要将按钮或菜单项添加到其他人的应用程序(电子邮件,Gmail,消息等),您就不能 - 抱歉!

答案 1 :(得分:0)

这是一个非常广泛的问题,因为有很多方法可以添加按钮,它实际上只取决于您希望它在屏幕上显示的位置,以及何时。但是假设你想在屏幕底部有一个始终存在的按钮,你可能会这样做:

在你的布局xml中,你会做这样的事情:

    ?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>

然后,在活动的onCreate()中,您需要创建与按钮关联的对象,并定义其行为

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
   }
});

我希望这可以帮助你

相关问题