弹出菜单

xiaoxiao2021-02-28  41

布局代码:

菜单文件:

完整代码:

public class MainActivity extends Activity { private TextView tvMsg; private float fontSize = 15.0f; @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tvMsg = (TextView) findViewById(R.id.tv);        tvMsg.setTextSize(fontSize);    } public void showPopup(View v){ //锚点 PopupMenu popup = new PopupMenu(this,v); getMenuInflater().inflate(R.menu.main, popup.getMenu()); //显示菜单 popup.show(); //处理菜单项的点击事件 popup.setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.action_add: fontSize+=5; break; case R.id.action_sub: fontSize-=5; break; } tvMsg.setTextSize(fontSize); return false; } }); }}

转载请注明原文地址: https://www.6miu.com/read-2350387.html

最新回复(0)