[英]How to add button in header
[原样图像] http://sdrv.ms/1bx8Bxm [待生成图像] http://sdrv.ms/1aGADfo我们想要将维护按钮从上下文移动到标题。 我们尝试了很多方法。 一些示例代码,但是一旦添加到代码中就会出错。 任何人都可以帮助我们吗? 第一张图片就是我们现在的样子,第二张图片就是我们想要达到的。 非常感谢!!!
我们在xml中用于维持按钮的代码如下:
<Button
android:id="@+id/maintain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Maintain"
android:background="#F77D03" />
将此代码放在资源main1.xml的菜单文件夹中
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_maintain"
android:showAsAction="always"
android:title="Maintain"/>
</menu>
这在Activity
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main1, menu);// Menu Resource, Menu
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case R.id.action_maintain:
Toast.makeText(getApplicationContext(), "Item 1 Selected", Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.