来简单介绍一下android中子目录(在xml文件中实现而非submenu)以及上下文菜单(OnContextMenu)、选项菜单(OnOptionsMenu)、以及Popoup啥的。。。
(Demo基于Android Studio完成,复制源码请注意先将string以及color文件夹下内容进行复制)
1.将左侧栏的文件预览切换到project模式,在res文件夹下新建一个menu文件夹,之后建立contextmenu、optionmenudemo、popupmenu三个目录文件;各个文件的代码分别如下:
contextmenu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/big"
app:showAsAction="never"
android:title="@string/big"/>
<item
android:id="@+id/middle"
app:showAsAction="never"
android:title="@string/middle"/>
<item
android:id="@+id/small"
app:showAsAction="never"
android:title="@string/small"/>
</menu>
optionmenudemo:
此处直接在代码中实现了子菜单,在item标签下再编写menu标签达到子菜单效果:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
app:showAsAction="never"
android:id="@+id/set_bg"
android:title="@string/menu1">
<menu>
<item
app:showAsAction="never"
android:id="@+id/spring"
android:title="@string/spring"/>
<item
app:showAsAction="never"
android:id="@+id/summer"
android:title="@string/summer"/>
<item
app:showAsAction="never"
android:id="@+id/autumn"
android:title="@string/autumn"/>
<item
app:showAsAction="never"
android:id="@+id/winter"
android:title="@string/winter"/>
</menu>
</item>
<item
android:id="@+id/quit"
android:title="@string/quit" />
</menu>
popupmenu:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/red"
android:title="@string/red"/>
<item
android:id="@+id/green"
android:title="@string/green"/>
<item
android:id="@+id/blue"
android:title="@string/blue"/>
</menu>
附上strings文件中内容:
<resources>
<string name="app_name">AndroidHomeWork</string>
<string name="testTv">这是我的第一个文本测试内容</string>
<string name="homework1">
叮叮当,叮叮当,铃儿响叮当\n
我们滑雪多快乐,我们坐在雪橇上\n
冲过大风雪,他们坐在雪橇上\n
奔驰过田野,欢笑又歌唱\n
铃声响叮当,你的精神多欢畅\n
今晚滑雪真快乐把滑雪歌儿唱\n
叮叮当,叮叮当,铃儿响叮当\n
我们滑雪多快乐,我们坐在雪橇上
</string>
<string name="menu1">设置背景</string>
<string name="quit">退出</string>
<!-- 显示季节的字符资源-->
<string name="spring">希望之春</string>
<string name="summer">宁静之夏</string>
<string name="autumn">收获之秋</string>
<string name="winter">晶莹之冬</string>
<!-- 显示字符颜色的资源-->
<string name="red">红色</string>
<string name="green">绿色</string>
<string name="blue">蓝色</string>
<!-- 显示字符大小的资源-->
<string name="big">大字号</string>
<string name="middle">中字号</string>
<string name="small">小字号</string>
</resources>
values下color文件中内容:
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="red">#FF0000</color>
<color name="green">#00FF00</color>
<color name="blue">#0000FF</color>
<color name="yellow">#FFFF00</color>
</resources>
layout文件源码:
<LinearLayout
android:id="@+id/layout_1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_gravity="center"
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/homework1" />
</LinearLayout>
现在进行MainActivity中代码的编写:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tv1;
private LinearLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//使layout绑定到id为layout_1的LinearLayout
layout = (LinearLayout) findViewById(R.id.layout_1);
//使tv1绑定到id为tv1的TextView
tv1 = (TextView) findViewById(R.id.tv1);
tv1.setOnClickListener(this);
//长按事件监听:
tv1.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//展示出ContextMenu
v.showContextMenu();
return true;
}
});
//注册上下文菜单
registerForContextMenu(tv1);
}
/**
* 用于切换项目背景图片
* @param menu
* @return
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//创建右上角的隐藏式菜单
getMenuInflater().inflate(R.menu.optionmenudemo,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//onOptionsMenu的响应事件
switch (item.getItemId()){
case R.id.spring:
layout.setBackgroundResource(R.mipmap.bg_spring);
break;
case R.id.summer:
layout.setBackgroundResource(R.mipmap.bg_summer);
break;
case R.id.autumn:
layout.setBackgroundResource(R.mipmap.bg_autumn);
break;
case R.id.winter:
layout.setBackgroundResource(R.mipmap.bg_winter);
break;
case R.id.quit:
finish();
default:
}
return super.onOptionsItemSelected(item);
}
/**
* 用于显示PopMenu
*/
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.tv1:
showPopMenu(v);
}
}
/**
* 编写showPopMenu的展示方法,编写了点击popup目录之后的响应事件
* @param v
*/
public void showPopMenu(View v){
PopupMenu menu = new PopupMenu(this,v);
menu.getMenuInflater().inflate(R.menu.popupmenu,menu.getMenu());
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.green:
tv1.setTextColor(getResources().getColor(R.color.green));
break;
case R.id.red:
tv1.setTextColor(getResources().getColor(R.color.red));
break;
case R.id.blue:
tv1.setTextColor(getResources().getColor(R.color.blue));
}
return true;
}
});
menu.setOnDismissListener(new PopupMenu.OnDismissListener() {
@Override
public void onDismiss(PopupMenu menu) {
}
});
menu.show();
}
/**
* 创建上下文菜单
*/
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.contextmenu,menu);
}
/**
* 上下文菜单的响应事件
*/
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.big:
tv1.setTextSize(30);
break;
case R.id.middle:
tv1.setTextSize(20);
break;
case R.id.small:
tv1.setTextSize(15);
break;
default:
}
return true;
}
}
网友评论