kotlin for android 之 给项目添加菜单栏
好像好多手机app都有个菜单栏,我们也弄一个。还是修改我们原来的项目,在res目录下新建个menu目录,然后选menu resourse file 新建my_menu.xml文件,如图:然后修改此文件:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/idpicture1" android:title="picture1" />
<item android:id="@+id/idpicture2" android:title="picture2" />
</menu>
修改MainActivity.kt文件:package com.example.xinwei.imageview
import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.graphics.PorterDuff
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main2.*
class MainActivity : AppCompatActivity(),View.OnClickListener{
var flag:Int=0
override fun onClick(p0: View?) {
when(p0?.id){
idlight.id->idimage1.setColorFilter(Color.CYAN,PorterDuff.Mode.DST_OVER)
iddark.id->idimage1.setColorFilter(Color.RED,PorterDuff.Mode.DARKEN)
idrb1.id->idimage1.setColorFilter(Color.CYAN,PorterDuff.Mode.DARKEN)
idrb2.id->idimage1.setColorFilter(Color.GREEN,PorterDuff.Mode.DARKEN)
}
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.my_menu,menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
when(item?.itemId){
R.id.idpicture1->{
idimage1.setImageResource(R.drawable.a0)
flag=0
}
R.id.idpicture2-> {
idimage1.setImageResource(R.drawable.a1)
flag=1
}
}
return true
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
idlight.setOnClickListener(this)
iddark.setOnClickListener(this)
idrb1.setOnClickListener(this)
idrb2.setOnClickListener(this)
idimage1.setOnClickListener {
var intent= Intent(this,Main2Activity::class.java)
when(idrdg.checkedRadioButtonId){
iddark.id->intent.putExtra("name",1)
idrb1.id->intent.putExtra("name",2)
idrb2.id->intent.putExtra("name",3)
}
intent.putExtra("pic",flag)
startActivity(intent)
}
}
}
这里面复写的两个方法一个是创建菜单,一个事给菜单设置点击事件的作用。然后我们再用原来的方法放一张图片到drawable目录下,修改Main2Activity.kt文件:
package com.example.xinwei.imageview
import android.graphics.Color
import android.graphics.PorterDuff
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main2.*
class Main2Activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
var id=intent.getIntExtra("name",0)
var pic=intent.getIntExtra("pic",0)
if(pic==1){
idimage2.setImageResource(R.drawable.a1)
}else{
idimage2.setImageResource(R.drawable.a0)
}
when(id){
1->idimage2.setColorFilter(Color.RED,PorterDuff.Mode.DARKEN)
2->idimage2.setColorFilter(Color.CYAN,PorterDuff.Mode.DARKEN)
3->idimage2.setColorFilter(Color.GREEN,PorterDuff.Mode.DARKEN)
}
}
}
这样我们的手机app就有2张图片可以选择了,如图:
页:
[1]