2016-02-27 41 views
0

我與在toolbar.I文本添加圖標越來越問題已經嘗試下面的代碼圖標:如何顯示與工具欄中的文本

public boolean onCreateOptionsMenu(Menu menu) { 
    menu.clear(); 
    MenuInflater menuInflater = getMenuInflater(); 
    menuInflater.inflate(R.menu.menu_new_group, menu); 
    return super.onCreateOptionsMenu(menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.createGroup: 
      createNewGroup(); 
      return true; 

     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 

menu_new_group.xml

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 
    <item 
     android:id="@+id/createGroup" 
     android:icon="@drawable/single_tick" 
     android:orderInCategory="100" 
     android:title="Create" 
     app:showAsAction="always|withText" /> 
</menu> 

工具欄中只顯示單個記號。標題不顯示。請幫忙 。

更新代碼:

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:id="@+id/createGroup" 
     android:icon="@drawable/single_tick" 
     android:orderInCategory="100" 
     android:showAsAction="withText|always" 
     android:title="Create" /> 
</menu> 

還沒工作me.Please查看下面的截圖:不顯示

enter image description here

圖標希望這兒過得上的圖標和文字工具欄。

activity_new_group.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolBarNewGroup" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/toolbar_color" 
     android:minHeight="?attr/actionBarSize" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"></android.support.v7.widget.Toolbar> 

    <FrameLayout 
     android:id="@+id/frame" 
     android:layout_width="180dp" 
     android:layout_height="180dp" 
     android:layout_gravity="center" 
     android:layout_marginTop="@dimen/margin10"> 

     <com.almabay.almachat.circularImageView.CircularImageView 
      android:id="@+id/groupPic" 
      android:layout_width="130dp" 
      android:layout_height="130dp" 
      android:layout_gravity="bottom|center_horizontal" /> 

     <com.almabay.almachat.circularImageView.CircularImageView 
      android:id="@+id/iv_camera_group" 
      android:layout_width="60dp" 
      android:layout_height="60dp" 
      android:layout_gravity="top|right" 
      android:layout_marginTop="@dimen/margin30" 
      android:background="@drawable/color" 
      android:src="@drawable/camera1" /> 
    </FrameLayout> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="@dimen/margin10" 
     android:layout_marginTop="@dimen/margin20" 
     android:background="@drawable/edittext_bottom_border_orange" 
     android:hint="Type group subject here ..." 
     android:paddingBottom="@dimen/padding10" /> 

    <ListView 
     android:id="@+id/lv_friends" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"></ListView> 

</LinearLayout> 
+0

可以顯示輸出 –

+0

顯示我認爲你的創建按鈕與工具欄重疊。分享您的佈局代碼 –

回答

0

如果您想使用默認的菜單欄,您必須覆蓋佈局。否則,您可以使用Toolbar,這是完全可定製的,並且非常易於實施。

+0

我在此處使用工具欄並在此處添加僅顯示singletick圖標的菜單 –

+0

太好了。使用工具欄,而不是使用菜單xml文件,如@Shajeel所述,在您的工具欄佈局中添加菜單項。它更容易。 – Pedif

1

您可以用工具欄將TextView包裝如下,以完全自定義它。使用TextView,您可以使用drawableLeftdrawlableRight指定圖標。

<android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:minHeight="?attr/actionBarSize" 
     app:layout_collapseMode="pin" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
     app:theme="@style/ToolBarStyle"> 

     <TextView 
      android:id="@+id/spinner_nav" 
      android:drawableLeft="Your_Draable" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right"/> 
</android.support.v7.widget.Toolbar> 
+0

謝謝你的建議對我有用,但重力不起作用。我想將textview對齊到工具欄的右側。如何實現這一點? –

+0

嘗試'android:layout_gravity =「right」' –

+0

它不顯示在文本視圖中,也不適用於工具欄 –

1

我已經通過下面的代碼解決了這個問題:

1在菜單文件夾中創建菜單

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

<item 
    android:id="@+id/action_sign" 
    android:title="簽到" 
    app:showAsAction="always|withText" 
    app:actionLayout="@layout/menu_sign" /> 

2- app:actionLayout是關鍵詞,則創建命名佈局menu_sign並將textView放入其中。該圖標會由drawableLeft

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clickable="true" 
    android:drawableLeft="@mipmap/ic_user_sign" 
    android:gravity="center" 
    android:orientation="vertical" 
    android:text="@string/text" 
    android:drawablePadding="@dimen/margin_5" 
    android:paddingLeft="@dimen/margin_10" 
    android:paddingRight="@dimen/margin_10" 
    android:textColor="@android:color/white" /> 

3-添加監聽你的菜單

super.onCreateOptionsMenu(menu, inflater); 
    inflater.inflate(R.menu.menu_sign, menu); 
    signItem = menu.findItem(R.id.action_sign); 
    signItem.getActionView().setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      onOptionsItemSelected(signItem); 
     } 
    }); 

4-那麼你將得到菜單和偵聽