如何在Android的工具栏中添加刷新和搜索之类的按钮? [英] How to add buttons like refresh and search in ToolBar in Android?

查看:157
本文介绍了如何在Android的工具栏中添加刷新和搜索之类的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个工具栏,但是当我在menu.xml中添加菜单项时,它始终显示为溢出. 如何分别添加? 此外,标题显示在中间(垂直),我如何在顶部显示呢?

I have made a ToolBar, but when I add menu items in menu.xml it always shows as an overflow. How do I add it separately? Moreover the Title is shown in the middle (vertically), how do I show it at the top?

menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.toolbar.MainActivity" >

       <item android:id="@+id/action_search"
          android:icon="@drawable/ic_launcher"
          android:title="@string/action_search"
          android:showAsAction="ifRoom"
          android:orderInCategory="1" 
          android:menuCategory="secondary"/>

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never"/>

</menu>

MainActivity.java

MainActivity.java

package com.example.toolbar;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
        setSupportActionBar(toolbar);
       // toolbar.setNavigationIcon(R.drawable.back);
        toolbar.setLogo(R.drawable.ic_launcher);
        //toolbar.setTitle("Title");
       // toolbar.setSubtitle("Subtitle");





    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

activity_main.xml

activity_main.xml

<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"

    tools:context="com.example.toolbar.MainActivity" >

     <android.support.v7.widget.Toolbar
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:id="@+id/my_awesome_toolbar"
         android:layout_width="fill_parent"
         android:layout_height="128dp"
         android:layout_alignParentLeft="true"
         android:layout_alignParentTop="true"
         android:background="?attr/colorPrimary"
         android:minHeight="?attr/actionBarSize"
         app:theme="@style/ActionBarThemeOverlay">
     </android.support.v7.widget.Toolbar>



</RelativeLayout>

推荐答案

好,我得到了图标,因为我使用的是v7库,所以我是在menu.xml android:showAsAction="ifRoom"中而不是在app:showAsAction="ifRoom"中编写的.

OK, I got the icons because I wrote in menu.xml android:showAsAction="ifRoom" instead of app:showAsAction="ifRoom" since i am using v7 library.

但是标题位于扩展工具栏的中心.如何使其显示在顶部?

However the title is coming at center of extended toolbar. How to make it appear at the top?

这篇关于如何在Android的工具栏中添加刷新和搜索之类的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆