我是Android Studio的新手,我一直在尝试用自定义的工具栏替换默认的AppBar
。我正在关注developer.android.com/training/appbar教程,我已经做了一切,正确(我认为)。
问题在于,当我添加toolbar
时,它不会替换默认值,而是将其放置在下方。我已经按照教程的每一步进行操作,但它仍然是这样的。
答案 0 :(得分:0)
此代码将删除您的默认appbar。
将其放在 style.xml
上<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
在 menifests.xml 中进行如下编码:
<activity
android:name=".ReceiveSingleScanActivity"
android:theme="@style/AppTheme.NoActionBar"
/>
作为你的评论:
我已经解决了这个问题,但是现在显示了条形图,它并没有 显示应用标题或菜单
你的menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Single menu item
Set id, icon and Title for each menu item
-->
<item android:id="@+id/menu_bookmark"
android:icon="@drawable/icon_bookmark"
android:title="Bookmark" />
<item android:id="@+id/menu_save"
android:icon="@drawable/icon_save"
android:title="Save" />
<item android:id="@+id/menu_search"
android:icon="@drawable/icon_search"
android:title="Search" />
<item android:id="@+id/menu_share"
android:icon="@drawable/icon_share"
android:title="Share" />
<item android:id="@+id/menu_delete"
android:icon="@drawable/icon_delete"
android:title="Delete" />
<item android:id="@+id/menu_preferences"
android:icon="@drawable/icon_preferences"
android:title="Preferences" />
活动代码应该是这样的:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_take_inventory_result);
setToolbar();
views();
init();
}
//for initialize and set Title of toolbar
private void setToolbar() {
toolbarTop = (Toolbar) findViewById(R.id.toolbarAdjust);
setSupportActionBar(toolbarTop);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Count Inventory");
}
用于创建菜单:
// Initiating Menu XML file (menu.xml)
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
/**
* Event Handling for Individual menu item selected
* Identify single menu item by it's id
* */
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_bookmark:
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
Toast.makeText(AndroidMenusActivity.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_save:
Toast.makeText(AndroidMenusActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_search:
Toast.makeText(AndroidMenusActivity.this, "Search is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_share:
Toast.makeText(AndroidMenusActivity.this, "Share is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_delete:
Toast.makeText(AndroidMenusActivity.this, "Delete is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_preferences:
Toast.makeText(AndroidMenusActivity.this, "Preferences is Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
答案 1 :(得分:0)
根据@Abhishek kumar答案中的评论,为您的新问题添加答案:
我已经解决了问题,但是现在显示了条形图,它既没有显示应用标题也没有显示菜单
要在自定义工具栏上显示应用标题,有两种方法可以实现:
第一种方法:在 AndroidManifest.xml 中,您可以添加 android:label 的值:
<activity
android:name=".ReceiveSingleScanActivity"
android:label="ReceiveSingleScanActivity"
<!-- (or any string from strings.xml could be passed here as well) -->
android:theme="@style/AppTheme.NoActionBar" />
第二种方法:您还可以使用以下代码在活动类中设置应用标题:
getSupportActionBar().setTitle("ReceiveSingleScanActivity");
使用official documentation显示自定义操作栏上的菜单,您需要:
首先,按照上面的文档中的描述创建菜单xml(命名为menu.xml)文件。
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- "Mark Favorite", should appear as action button if possible -->
<item
android:id="@+id/action_favorite"
android:icon="@drawable/ic_favorite_black_48dp"
android:title="@string/action_favorite"
app:showAsAction="ifRoom"/>
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
app:showAsAction="never"/>
</menu>
然后在您的活动中覆盖 onCreateOptionsMenu(菜单菜单) 方法,以夸大活动中的菜单。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu (menu.xml); this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
最后在您的活动中覆盖 onOptionsItemSelected(MenuItem item) 方法,以便为菜单中的按钮添加操作。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.action_settings:
// User chose the "Settings" item, show the app settings UI...
return true;
case R.id.action_favorite:
// User chose the "Favorite" action, mark the current item
// as a favorite...
return true;
default:
// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
return super.onOptionsItemSelected(item);
}
}