如何在运行时修改 WearableActionDrawer?
How to modify WearableActionDrawer on runtime?
我已经为我的 Wear OS 应用程序创建了一个 ActionDrawer,并且想在 运行 时间更改图标和文本。此代码有效,我的应用程序继续 运行 没有错误,但我无法再向上滑动 ActionDrawer。此外,当 ActionDrawerMenu 已经打开并且我按下那里的按钮时,它消失了并且我的应用程序 UI 似乎被冻结了。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Top navigation drawer
WearableNavigationDrawerView wearableNavigationDrawer = (WearableNavigationDrawerView) findViewById(R.id.top_navigation_drawer);
wearableNavigationDrawer.setAdapter(new NavigationDrawerAdapter());
wearableNavigationDrawer.getController().peekDrawer();
//Bottom action drawer
WearableActionDrawerView wearableActionDrawer = (WearableActionDrawerView) findViewById(R.id.bottom_action_drawer);
wearableActionDrawer.getController().peekDrawer();
wearableActionDrawer.setOnMenuItemClickListener(this);
}
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
final int itemId = menuItem.getItemId();
switch(itemId) {
case R.id.menuItem_pause:
if(intent == null) {
menuItem.setIcon(getDrawable(R.drawable.ic_pause));
menuItem.setTitle(R.string.actionDrawerMenuPause);
} else {
menuItem.setIcon(getDrawable(R.drawable.ic_play));
menuItem.setTitle(R.string.actionDrawerMenuStart);
}
invalidateOptionsMenu();
return true;
}
return false;
}
我尝试了一些使用 invalidateOptionsMenu()、onPrepareOptionsMenu() 和 onCreateOptionsMenu() 的解决方案,但对我没有任何效果。我认为这只适用于标准 Android 移动应用程序,但不适用于 Wear OS。那么如何在按下 MenuItem 时更改 WearableActionDrawer-MenuItems 的文本和图标?
我无意中找到了解决方案。只是这一行代码以编程方式实现了菜单。
//Bottom action drawer
WearableActionDrawerView wearableActionDrawer = findViewById(R.id.bottom_action_drawer);
wearableActionDrawer.getController().peekDrawer();
wearableActionDrawer.setOnMenuItemClickListener(this);
getMenuInflater().inflate(R.menu.action_drawer_menu, wearableActionDrawer.getMenu());
我已经为我的 Wear OS 应用程序创建了一个 ActionDrawer,并且想在 运行 时间更改图标和文本。此代码有效,我的应用程序继续 运行 没有错误,但我无法再向上滑动 ActionDrawer。此外,当 ActionDrawerMenu 已经打开并且我按下那里的按钮时,它消失了并且我的应用程序 UI 似乎被冻结了。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Top navigation drawer
WearableNavigationDrawerView wearableNavigationDrawer = (WearableNavigationDrawerView) findViewById(R.id.top_navigation_drawer);
wearableNavigationDrawer.setAdapter(new NavigationDrawerAdapter());
wearableNavigationDrawer.getController().peekDrawer();
//Bottom action drawer
WearableActionDrawerView wearableActionDrawer = (WearableActionDrawerView) findViewById(R.id.bottom_action_drawer);
wearableActionDrawer.getController().peekDrawer();
wearableActionDrawer.setOnMenuItemClickListener(this);
}
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
final int itemId = menuItem.getItemId();
switch(itemId) {
case R.id.menuItem_pause:
if(intent == null) {
menuItem.setIcon(getDrawable(R.drawable.ic_pause));
menuItem.setTitle(R.string.actionDrawerMenuPause);
} else {
menuItem.setIcon(getDrawable(R.drawable.ic_play));
menuItem.setTitle(R.string.actionDrawerMenuStart);
}
invalidateOptionsMenu();
return true;
}
return false;
}
我尝试了一些使用 invalidateOptionsMenu()、onPrepareOptionsMenu() 和 onCreateOptionsMenu() 的解决方案,但对我没有任何效果。我认为这只适用于标准 Android 移动应用程序,但不适用于 Wear OS。那么如何在按下 MenuItem 时更改 WearableActionDrawer-MenuItems 的文本和图标?
我无意中找到了解决方案。只是这一行代码以编程方式实现了菜单。
//Bottom action drawer
WearableActionDrawerView wearableActionDrawer = findViewById(R.id.bottom_action_drawer);
wearableActionDrawer.getController().peekDrawer();
wearableActionDrawer.setOnMenuItemClickListener(this);
getMenuInflater().inflate(R.menu.action_drawer_menu, wearableActionDrawer.getMenu());