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

时间:2014-11-06 11:46:07

标签: android-5.0-lollipop android-toolbar

enter image description here

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

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

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中

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

4 个答案:

答案 0 :(得分:79)

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

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

答案 1 :(得分:5)

尝试这样做:

#unsupported-browser-modal {

  .modal-dialog, .modal-content {
    height: 100%;
  }

  .modal-dialog {
    width: 100%;
    margin: 0;
    max-width: none;
    min-width: none;
  }

  .modal-content {
    border-bottom: 0;
  }

  .modal-header {
    border-bottom: 0;
    padding: 15px;
    min-height: 16.42857143px;
  }

  .modal-title {
      font-family : $var;
      text-align     : center;
      font-weight    : bold;
      letter-spacing : 2px;
      margin-top: 50px;
  }

  .modal-body {
    font-family: $var1;
    font-size: 14px;
    text-align: center;
    padding: 2px 35px 80px;
    color: #646464;
    line-height: 20px;
  }
}

#unsupported-browser-modal.modal {
  display: block;
}

.modal-text {
  top: 37%;
  position: relative;
}

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

getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);

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

答案 2 :(得分:3)

在顶部添加此行:

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

然后使用:

app:showasaction="ifroom"

答案 3 :(得分:0)

要控制标题的位置,您可能需要设置自定义字体,如此处所述(通过twaddington):Link

然后要重新定位文字的位置,在updateMeasureState()中添加p.baselineShift += (int) (p.ascent() * R); 同样在updateDrawState()添加tp.baselineShift += (int) (tp.ascent() * R); 其中R是介于-1和1之间的两倍。