android学习

来源:互联网 发布:js如何控制embed播放 编辑:程序博客网 时间:2024/06/06 04:14

AndroidManifest.xml里注册的活动是不能使用的。其中intent一filter里的两行代码非常重要,

<action android:name="android.intent.action.MAIN"/>
 <category android:name="android.intent.category.LAUNCHER"/>

表示是这个项目的主活动,在手机上点击应用图标,首先启动的就是这个活动。


android:layout_width="match_parent"   表示当前元素和父元素一样宽.
android:layout_height="wrap_content"  表示当前元素的高度只要能刚好包含里面的内容就行


隐藏标题栏:

this.requestWindowFeature(Window.FEATURE_NO_TITLE);  


菜单:

定义菜单:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
   android:id="@+id/add_item"
   android:title="Add"/> 
<item
   android:id="@+id/remove_item"
   android:title="Remove"/>
</menu>


调用菜单:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


通过getMenulnflater()方法能够得到Menulnflater对象,再调用它的inflate()方法就可以给当前活动创建菜单了。innate()方法接收两个参数,第一个参数用于指定我们通过哪一个资源文件来创建菜单,这里当然传入R.menu.main,第二个参数用于指定我们的菜单项将添加到哪一个Menu对象当中,这里直接使用onCreateoptionsMenu()方法中传入的menu参数。然后给这个方法返回true,表示允许创建的菜单显示出来,如果返回了false,创建的菜单将无法显示。当然,仅仅让菜单显示出来是不够的,我们定义菜单不仅是为了看的,关键是要菜单真正可用才行,因此还要再定义菜单响应事件。在FirstActivity中重写onoptionsltemselected()
方法:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch(item.getItemId())
        {
        case R.id.add_item:
        Toast.makeText(this, "you click Add",Toast.LENGTH_SHORT).show();
        break;
        case R.id.remove_item:
        Toast.makeText(this, "you click Remove",Toast.LENGTH_SHORT).show();
        break;
        default:
        }
        return true;
    }



每个Intent能指定一个action,但却能指定多个category


0 0
原创粉丝点击