Android 如何添加操作栏
我想在活动顶部添加操作栏。如何在以下布局中添加操作栏。 我想为低于11级的API创建操作栏。谁能给我提供这方面的教程参考 布局-Android 如何添加操作栏,android,android-layout,android-actionbar,Android,Android Layout,Android Actionbar,我想在活动顶部添加操作栏。如何在以下布局中添加操作栏。 我想为低于11级的API创建操作栏。谁能给我提供这方面的教程参考 布局- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@d
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/buddha"
android:gravity="center"
android:orientation="vertical"
>
<View
android:id="@+id/top1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:background="@null"
android:gravity="center"
android:layout_weight="5"
/>
<Button
android:id="@+id/hist"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:background="#73000000"
android:gravity="center"
android:textSize="@dimen/btxt"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:layout_weight="1"
android:text="@string/his"
/>
<View
android:id="@+id/top2"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:background="@null"
android:gravity="center"
android:layout_weight=".10"
/>
<Button
android:id="@+id/typ"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:gravity="center"
android:background="#73000000"
android:textSize="@dimen/btxt"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:layout_weight="1"
android:text="@string/typ"
/>
<View
android:id="@+id/top3"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:background="@null"
android:gravity="center"
android:layout_weight=".10"
/>
<Button
android:id="@+id/ben"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:gravity="center"
android:layout_weight="1"
android:textSize="@dimen/btxt"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:background="#73000000"
android:text="@string/ben"
/>
<View
android:id="@+id/top4"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:background="@null"
android:gravity="center"
android:layout_weight=".10"
/>
<Button
android:id="@+id/exit"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:gravity="center"
android:textSize="@dimen/btxt"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:background="#73000000"
android:layout_weight="1"
android:text="@string/exit"
/>
<View
android:id="@+id/top41"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:background="@null"
android:gravity="center"
android:layout_weight=".10"
/>
</LinearLayout>
兼容性库支持在旧设备上运行的actionbar。这篇博文解释了如何使用它,并提供了代码示例:将您的项目设置为appcompat v7库以供参考,请参见和
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME
| ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);
}
您需要在
res/menu
文件夹中创建menu.xml
文件
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_refresh"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="@drawable/ic_action_refresh"
android:title="Refresh"/>
<item
android:id="@+id/action_settings"
android:title="Settings">
</item>
</menu>
由于您希望在API级别低于11的情况下使用操作栏,所以有两个流行的选项
- 使用
- 或者android支持库中的ActionBarCompat库(您可以在android开发者页面中找到)
ActionBarSherlock教程:尝试使用支持库。请查看上面提供的链接中的示例部分,您将了解如何在应用程序中添加actionbarsherlock。我想您必须检查以下内容:之后使用适用于<3.0 android操作系统的actionbarsherlock库或actionbarcompat也检查一下这一点。使用
AppCompat
库而不是actionbarsherlock
?操作栏不是xml布局的一部分。它是由系统添加的,如果您扩展,您还必须更改应用程序的样式以使用Theme.AppCompat。请检查我在回复中添加的链接,因为它提供了所有步骤。@PareshMayani您能告诉我任何链接来解释如何在特定位置的actionbar中添加图标吗?感谢您提供上述链接。@JohnR阅读本文可能会对您有所帮助
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}