默认情况下,android有一个选项,可以在工具栏的左侧启用BACK ARROW
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
就像我需要工具栏右侧的TICK MARK一样,是否有任何方法可以在Android中默认启用TICK MARK。
首先从materials icon library下载“完成”图标作为png
将图标复制到项目可绘制文件夹。
然后在res / menu中创建一个名为menu_example.xml的文件
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:icon="@drawable/ic_done_white_24dp"
android:title="@string/action_done"
app:showAsAction="always" />
</menu>
然后将以下代码添加到您的Activity。
@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_example, menu);
return true;
}
要为tick打开一个onClick侦听器,您可以实现:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_example, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);
toast.show();
return true;
}