在我的 activity_main.xml 中,我有这个工具栏:
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#770000ff"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
在清单中有 <application>
:
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
在 res/values/style.xml
我有
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
MainActivity.java
文件是:
import ... ;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
}
}
现在,工具栏只有应用程序名称,没有其他内容。我如何添加带有图标的项目,例如 http://i.stack.imgur.com/y29jV.png ?
原文由 SctALE 发布,翻译遵循 CC BY-SA 4.0 许可协议
首先是一个 Menu xml 文件并将其放在 res/menu 文件夹中
在活动文件中添加这些方法