当前位置: 移动技术网 > 移动技术>移动开发>Android > Android ToolBar控件详解及实例

Android ToolBar控件详解及实例

2019年07月24日  | 移动技术网移动技术  | 我要评论

toolbar控件详解

在activity中添加toolbar

p1

1.添加库

dependencies {
  ...
  compile "com.android.support:appcompat-v7:18.0.+"
}

2.activity要继承appcompatactivity

3.设置主题

使用toolbar,要将系统默认的actionbar隐藏掉

<application
  android:theme="@style/theme.appcompat.light.noactionbar"
  />

4.在xml添加toolbar布局

<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:theme="@style/themeoverlay.appcompat.actionbar"
  app:popuptheme="@style/themeoverlay.appcompat.light"/>

material design specification 建议设置 elevation 为 4dp

toolbar放在activity的顶部(等于废话)

5.设置toolbar

在activity的oncreate()方法中,调用setsupportactionbar()方法,把我们的toolbar对象传递进去,将我们xml中添加的toolbar设置为我们这个activity页面的app bar。

@override
protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_my);
  toolbar mytoolbar = (toolbar) findviewbyid(r.id.my_toolbar);
  setsupportactionbar(mytoolbar);
  }

现在,我们的页面中,应该就会有一个app bar了,默认情况下,这个app bar只会显示一个app的名字和一个带有下拉选项的按钮,也可以在app bar上添加更多的选项按钮

添加并处理一个action

toolbar允许在上面添加操作按钮,但是因为toolbar上空间是有限的,如果一个程序有太多的action,那么可以将action添加到下拉菜单中,而不显示在toolbar上

p2

添加一个action按钮

可以在xml文件中定义所有我们想添加的操作按钮和下拉列表中的行为,如果想要添加action,可以在 res/目录下创建一个新的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>

app:showasaction 属性是用来设置action显示在哪,如果我们设置 app:showasaction =”ifroom”(示例中最常用的一种方式) ,这个action将显示在toolbar上,如果toolbar上没有足够的控件,则显示在下拉菜单中。如果在程序中设置为 app:showasaction=”never”,那么这个action将永远显示在下拉列表中,而不会显示在toolbar上。
响应操作(回调)

当用户选择了一个action后,系统会回调 onoptionsitemselected() 方法并传递 menuitem 对象,在 onoptionsitemselected() 的实现中,调用 menuitem.getitemid() 方法来确定按下了哪个项目。返回的 id 与您在相应的 元素 android: id 属性中声明的值匹配。

例如下面

@override
public boolean onoptionsitemselected(menuitem item) {
  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);

  }
}

添加一个回到主界面的按钮

应用为了使用户可以方便的回到主界面,toolbar可以添加一个按钮,直接返回到指定的主界面。

g1

声明主界面(父界面)

需要在清单文件中通过设置 android: parentactivityname 属性声明,如果要支持旧版本的android设备,需要定义 ,类似这样:

<application ... >
  ...

  <!-- the main/home activity (it has no parent activity) -->

  <activity
    android:name="com.example.myfirstapp.mainactivity" ...>
    ...
  </activity>

  <!-- a child of the main activity -->
  <activity
    android:name="com.example.myfirstapp.mychildactivity"
    android:label="@string/title_activity_child"
    android:parentactivityname="com.example.myfirstapp.mainactivity" >

    <!-- parent activity meta-data to support 4.0 and lower -->
    <meta-data
      android:name="android.support.parent_activity"
      android:value="com.example.myfirstapp.mainactivity" />
  </activity>
</application>

使用返回按钮

若要使用返回的按钮,需要调用setdisplayhomeasupenabled()方法,类似这样:

@override
protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_my_child);

  // my_child_toolbar is defined in the layout file
  toolbar mychildtoolbar =
    (toolbar) findviewbyid(r.id.my_child_toolbar);
  setsupportactionbar(mychildtoolbar);

  // get a support actionbar corresponding to this toolbar
  actionbar ab = getsupportactionbar();

  // enable the up button
  ab.setdisplayhomeasupenabled(true);
}

添加action views

和上面 添加并处理一个action 类似,只不过设置 showasaction 为 “ifroom|collapseactionview” 或者 “never|collapseactionview” 就可以了,类似这样

<item android:id="@+id/action_search"
   android:title="@string/action_search"
   android:icon="@drawable/ic_search"
   app:showasaction="ifroom|collapseactionview"
   app:actionviewclass="android.support.v7.widget.searchview" />

p3

这里我们添加的是一个带有搜索功能的search按钮,如果我们要自定义一个自己的按钮,不添加 actionviewclass 即可

如果想要配置这个action,可以在 oncreateoptionsmenu() 的回调里通过 getactionview() 获取到search对象,类似这样:

@override
public boolean oncreateoptionsmenu(menu menu) {
  getmenuinflater().inflate(r.menu.main_activity_actions, menu);

  menuitem searchitem = menu.finditem(r.id.action_search);
  searchview searchview = (searchview) menuitemcompat.getactionview(searchitem);

  // configure the search info and add any event listeners...

  return super.oncreateoptionsmenu(menu);6 
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网