您的位置:首页 > 移动开发 > Android开发

Android学习之界面篇(八)在项目中使用Action Bar

2016-05-05 18:08 246 查看
关于ActionBar
ActionBar是个窗体功能来鉴别用户当前在app中的位置,提供给用户一些功能和导航。通过ActionBar可以方便的让系统自动的适配不同尺寸的屏幕。

谷歌开发者文档介绍:http://developer.android.com/training/appbar/setting-up.html



上图所示就是Action Bar的应用。接下来我们在自己的项目中添加一个Action
Bar。


一、启用Action
Bar


1.在项目中导入appcompat-v7包,在Android
Studio当中导入包的步骤如下:

在项目上右键->Open Module Settings->Dependencies->点击加号添加appcompat-v7包。
2.使要显示的Activity继承自AppCompatActivity。

public class MainActivity extends AppCompatActivity {

3.修改AndroidManifest中的App主题

android:theme="@style/Theme.AppCompat.Light.NoActionBar"

4.在布局文件中添加ToolBar标签,界面配置如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.icarus.actionbarusing.MainActivity">

<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:layout_alignParentTop="true"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

</android.support.v7.widget.Toolbar>
</RelativeLayout>


5.在Activity的onCreate方法中使用setSupportActionBar方法来显示ActionBar。

Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);

这样一个带有Action Bar的Activity界面创建完成,效果如图:





二、在Action Bar上添加按钮

1.在项目中的res目录下新建menu目录,并创建一个新的main.xml文件

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto">

<!-- "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/favorite"

app:showAsAction="ifRoom"/>

<!-- Settings, should always be in the overflow -->

<item android:id="@+id/action_settings"

android:title="@string/settings"

app:showAsAction="never"/>

</menu>

这里所用到的图标资源可以到andrid官方提供的网站下载https://design.google.com/icons/

2.对菜单按钮的点击做出相应,实现onOptionsItemSelected 方法。

public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()){

case R.id.action_favorite:

//favorite对应操作

return true;

case R.id.action_settings:

//setting对应操作

return true;

default:

//默认操作

return super.onOptionsItemSelected(item);

}

}

现在我们应用的界面是这样的:



三、为当前的Activity添加向上按钮

这里我们要理解向上按钮与返回按钮的区别:

向上按钮偏向与Activity的父子关系

返回按钮则偏向与前后关系

1.想要给Activity上添加向上按钮,那么必须在AndroidManifest文件中声明其对应的父级Activity。

首先我们创建一个ParentActivity来作为当前Activity的父级Activity。

android:parentActivityName=".ParentActivity"

2.使向上按钮可见,在onCreate方法中执行setDisplayHomeAsUpEnabled 传入true来启用向上按钮

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

3.我们使用ParentActivity来启动MainActivity,在MainActivity界面中点击向上按钮来返回到ParentActivity中

findViewById(R.id.btnStartChild).setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

startActivity(new Intent(ParentActivity.this,MainActivity.class));

}

});



[b]四、添加Action Views和Action Providers[/b]

1.添加一个Action Views。

To add an action view, create an
<item>
element
in the toolbar's menu resource, as Add Action Buttonsdescribes. Add one of the following two
attributes to the
<item>
element:

actionViewClass
: The class of a widget that implements the action.
actionLayout
: A layout resource describing the action's components.

Set the
showAsAction
attribute to either
"ifRoom|collapseActionView"
or
"never|collapseActionView"
.
The
collapseActionView
flag indicates how to display the widget when the user is not interacting with it: If the widget is on the app bar, the app should display
the widget as an icon. If the widget is in the overflow menu, the app should display the widget as a menu item. When the user interacts with the action view, it expands to fill the app bar.

我们来添加一个搜索的按钮到Action Bar中。
首先在菜单中添加一个item

<item android:id="@+id/action_search"

android:title="@string/search"

android:icon="@drawable/ic_search_black_48dp"

app:showAsAction="ifRoom|collapseActionView"

app:actionViewClass="android.support.v7.widget.SearchView" />

这样,我们的界面上就出现了一个搜索图标,如图所示:



现在我们点击搜索图标,会在Action Bar上直接显示输入框。



2.添加一个Action Provider。

Action Provider是Action Bar的扩展部分,算是一个Action View的存在,

所以我们直接在menu中新建一个item来展示分享的Action Provider。

<item android:id="@+id/action_share"

android:title="@string/share"

app:showAsAction="ifRoom"

app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>

先去掉我们之前做的那个收藏的Button,只留下搜索和分享按钮。



到此,我们的Action Bar也就实现了全部的界面配置,具体的功能实现则需要在代码中进行相应的处理。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: