好吧,它可能是关于工具栏,让我们看看它是如何工作的。
在Android是更好地使用控件工具,而不是动作条,因为......
要做到这一点,你必须添加(如果没有)的appcompact V7库摇篮(模块:应用程序),就这样:
dependecies { compile 'com.android.support:appcompat-v7:+' }
要删除操作栏,并插入一个工具栏,你必须:
1. remove actionbar from manifest setting a no actionbar style:
<application
android:theme="@style/Theme.AppCompact.Light.NoActionBar"
/>
2. insert Toolbar widget into the layout of the activity:
<LinearLayout ...>
...
<android.support.v7.widget.Toolbar
android:id="@+id/mytoolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="8dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</LinearLayout>
3. insert the widget into the activity and set it as support toolbar:
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.mytoolbar);
myToolbar.setLogo(R.mipmap.ic_launcher);
myToolbar.setTitle("Titolo");
myToolbar.setSubtitle(R.string.sottotitolo);
setSupportActionBar(myToolbar);
}
4. create a menù for the Toolbar, into res>menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_game"
android:icon="@drawable/ic_new_game"
android:title="@string/new_game"
android:showAsAction="ifRoom"/>
<item android:id="@+id/help"
android:icon="@drawable/ic_help"
android:title="@string/help"
android:showAsAction="never"/>
<item android:id="@+id/tutorials"
android:icon="@drawable/ic_tuts"
android:title="@string/tutorials"
android:showAsAction="always"/>
</menu>
5. bind menù to toolbar through the two methods into the 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_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
newGame();
return true;
case R.id.help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
你真的应该读作[问]你张贴问题之前。这个问题几乎可以肯定会关闭,因为你没有发布任何源代码,也没有显示你得到的错误信息。 – Tibrogargan
我发布的GitHub页面基本上是我正在使用的确切代码。正如我所说,我改变的唯一的一些变量名称和一些drawable。此外,我得到的错误(正如我再次说)不存在。我没有收到错误。试图打开时,应用程序崩溃。这只是一个“不幸的是,___已经关闭”。 – Xxero
你改变了一些破坏代码的东西,并期望人们随机猜测你是做什么的? – Tibrogargan