2014-11-06 19 views
60

enter image description here如何在Android中的ToolBar中添加按鈕,如刷新和搜索?

我做了一個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> 
+0

發表您的menu.xml文件 – 2014-11-06 13:56:29

回答

77

OK,我得到了圖標,因爲我在menu.xml文件android:showAsAction="ifRoom"寫,而不是app:showAsAction="ifRoom"因爲我使用的是v7庫。

但是標題正在擴展工具欄的中心。如何讓它出現在頂部?

+0

嘗試使用機器人:重力= 「頂」 在您的工具欄。 – 2014-11-07 08:35:32

+0

沒有不工作.. – 2014-11-11 11:18:38

+8

你讓我的一天用「app:」而不是「android:」。從一見鍾情確實不太清楚。 – nucleo 2014-12-11 17:37:42

0

控制標題的位置可能要設置自定義的字體作爲說明如下(按twaddington):Link

然後重新定位文本的位置,在updateMeasureState()你會在添加p.baselineShift += (int) (p.ascent() * R); 同樣updateDrawState()tp.baselineShift += (int) (tp.ascent() * R); 其中R是-1和1

+1

這是複雜的。是否有任何屬性來設置它。 – 2015-02-06 06:27:51

5

嘗試之間雙重做到這一點:

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

,如果你做你的自定義工具欄(這我相信你所做的那樣),那麼你可以用最簡單的方法可以做到這一點:

toolbarTitle = (TextView)findViewById(R.id.toolbar_title); 
toolbarSubTitle = (TextView)findViewById(R.id.toolbar_subtitle); 
toolbarTitle.setText("Title"); 
toolbarSubTitle.setText("Subtitle"); 

你也一樣把你的工具欄上的任何其他意見。希望能幫助到你。

3

頂部添加這一行:

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

,然後使用:

app:showasaction="ifroom"

+0

這是爲什麼被拒絕?它爲我修好了 – likdaboo 2016-12-30 08:59:55

相關問題