【发布时间】:2017-10-18 14:06:14
【问题描述】:
我的应用中有一个工具栏,我想在应用执行期间(换句话说,在运行时)动态修改其内容。
例如,该应用能够拍摄和预览照片;一旦预览了这些照片,用户就可以选择一些照片并向服务器执行发送操作。我还想让用户在选择其中一些照片后能够删除照片,为此我希望操作栏上的“删除”项目(可通过垃圾桶图标识别)仅在一个或多个照片时可见照片被选中。
这可能吗? 如果是,如何?
换句话说,更一般地说,我想控制工具栏中的项目(可见性),将代码设置为仅当某些条件为“真”时它们才会“可见”(在上面的示例中,当照片是已选中,或者在另一个示例中,当用户登录时)并且当它们为“假”(当用户未登录时)时不可见。
现在我只需要在工具栏中“删除”(或使不可见)项目,但了解是否可以在运行时在工具栏中添加项目也很有用.
我正在添加一些有助于理解问题的代码。
app_bar.xml 文件在“/res/layout”中,以图形方式定义工具栏
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
android:theme="@style/ToolbarTheme" />
menu_resources.xml 文件,用于定义工具栏项
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- "User favourite function", should appear as action button if possible -->
<item
android:id="@+id/action_app_icon"
android:icon="@mipmap/ic_launcher"
android:title="@string/action_bar_app_icon"
app:showAsAction="always" />
<!-- Settings, should always be in the overflow -->
<item
android:id="@+id/action_delete"
android:icon="@drawable/trash"
android:title="@string/action_bar_delete"
app:showAsAction="always"/>
<!-- Settings, should always be in the overflow -->
<item
android:id="@+id/action_settings"
android:icon="@drawable/settings"
android:title="@string/action_bar_settings"
app:showAsAction="never"/>
<!-- About, should always be in the overflow -->
<item
android:id="@+id/about"
android:icon="@android:drawable/ic_dialog_info"
app:showAsAction="never"
android:title="@string/action_bar_about"/>
工具栏所在的 Activity 的一部分
public class myClass extends AppCompatActivity implements View.OnClickListener {
// Instantiating a toolbar
private Toolbar toolbar;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_class);
// Adding toolbar to the activity
toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
// Get a support ActionBar corresponding to this toolbar
ActionBar ab = getSupportActionBar();
// Enable the Up button
ab.setDisplayHomeAsUpEnabled(true);
}
// The method that creates an options 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.menu_resource, menu);
// This make the delete item invisible
menu.findItem(R.id.action_delete).setVisible(false);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
// Perform the settings action
return true;
case R.id.about:
// Perform the about
return true;
case R.id.action_delete:
deletePhotos();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public static void manageSelection(Boolean state, int position){
if (photoArray.getNumberSelected() == 0) {
// disable the trash icon and its functionality;
} else {
// enable the trash icon with its functionality;
}
}
// This method allows to deleteItems images to the array
public void deletePhotos() {
//code for deleting photos
}
}
感谢您的宝贵时间。
【问题讨论】:
标签: android dynamic runtime toolbar menuitem