首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在安卓的ToolBar中添加刷新和搜索等按钮?

如何在安卓的ToolBar中添加刷新和搜索等按钮?
EN

Stack Overflow用户
提问于 2014-11-06 19:46:07
回答 4查看 126.4K关注 0票数 62

我已经创建了一个ToolBar,但是当我在menu.xml中添加菜单项时,它总是显示为溢出。如何单独添加?此外,标题显示在中间(垂直),我如何在顶部显示它?

menu.xml

代码语言:javascript
复制
<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

代码语言:javascript
复制
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

代码语言:javascript
复制
<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>
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-11-07 13:28:17

好的,我得到这些图标是因为我用menu.xml android:showAsAction="ifRoom"而不是app:showAsAction="ifRoom"写的,因为我用的是v7库。

然而,标题出现在扩展工具栏的中心。如何让它出现在顶部?

票数 79
EN

Stack Overflow用户

发布于 2015-04-28 22:23:59

尝试这样做:

代码语言:javascript
复制
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);

如果你制作了你的自定义工具栏(我假设你做了),那么你可以使用最简单的方法来做到这一点:

代码语言:javascript
复制
toolbarTitle = (TextView)findViewById(R.id.toolbar_title);
toolbarSubTitle = (TextView)findViewById(R.id.toolbar_subtitle);
toolbarTitle.setText("Title");
toolbarSubTitle.setText("Subtitle");

放在工具栏中的任何其他视图也是如此。希望能有所帮助。

票数 5
EN

Stack Overflow用户

发布于 2016-09-06 15:19:50

在顶部添加以下行:

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

然后使用:

app:showasaction="ifroom"

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26778701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档