2016-10-15 7 views
-2

をアイコンを置く:今すぐ私はこのように、唯一のメニューアイコンを持つツールバーがあり、それがプログラムsettedされたツールバー

myToolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(myToolbar); 
    myToolbar.setTitleTextColor(Color.BLACK); 
    ActionBar ab = getSupportActionBar(); 
    ab.setHomeAsUpIndicator(R.drawable.menu); 
    ab.setDisplayHomeAsUpEnabled(true); 

、私は、ツールバーに別のアイコンを持っていると思いますし、これに私のsrcイメージとonClickメソッドを関連付けます。あなたが見ることができるように、私はXMLメニューファイルを持っていないし、また、デフォルトのアンドロイドアイコンを設定したくないので、どうすればいいですか?

答えて

0

ツールバーの主な目的はカスタムレイアウトを使用していることです。 ボタンなどのUI要素をツールバーに追加して、コード内で見つけることができます。

0

レイアウトファイルまたはViewオブジェクトを指定することで、CustomViewをActionBarに設定できます。そのカスタムビューでは、あなたが望むものを何でもすることができます。ここで

あなたはあなたがそこにsetup yourメニューとdefine any extra buttons (item)に別のXMLファイルを使用する必要があるとしているアクションバー

protected void setCustomActionBarTitle(String title) { 
     ActionBar actionBar = getActionBar(); 
     if (actionBar != null) { 
      actionBar.setDisplayHomeAsUpEnabled(true); 
      actionBar.setDisplayShowCustomEnabled(true); 
      actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
      actionBar.setCustomView(R.layout.layout_action_bar_title); 
      TextView titleView = (TextView) actionBar.getCustomView().findViewById(R.id.action_bar_title); 
      titleView.setText(title); 
      ImageView customIconImageView = (ImageView) actionBar.getCustomView().findViewById(R.id.custom_icon); 
      customIconImageView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        // call my method on the activity 
       } 
      }); 
      ImageView imageView = (ImageView) actionBar.getCustomView().findViewById(R.id.up_icon); 
      imageView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        onBackPressed(); 
       } 
      }); 
     } 
    } 
0

にカスタムビューを設定することができる方法です。ここではサンプルメニューファイルです:

<menu xmlns:android="http://schemas.android.com/apk/res/android" > 

    <!-- "Mark Favorite", should appear as action button if possible --> 
    <item 
     android:id="@+id/action_favorite" 
     android:icon="@drawable/ic_favorite_black_48dp" 
     android:title="@string/action_favorite" 
     app:showAsAction="ifRoom"/> 

    <!-- Settings, should always be in the overflow --> 
    <item android:id="@+id/action_settings" 
      android:title="@string/action_settings" 
      app:showAsAction="never"/> 

</menu> 
0

あなたのメニューにしたいアイコンを追加 - > menu_main.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=".ui.MainActivity"> 

<item 
    android:id="@+id/action_scan" 
    android:icon="@drawable/camera_icon" 
    android:orderInCategory="100" 
    android:title="@string/action_scan" 
    android:visible="true" 
    app:showAsAction="always" /> 

とあなたの活動では

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

このコードは私の仕事です。

setSupportActionBar(toolbar); 
    getSupportActionBar().setTitle(R.string.app_name); 
    toolbar.setTitleTextColor(getResources().getColor(R.color.colorWhite)); 
    toolbar.setNavigationOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      onBackPressed(); 
     } 
    }); 
    final ActionBar actionBar = getSupportActionBar(); 
    if (actionBar != null) { 
     actionBar.setDisplayHomeAsUpEnabled(true); 
    } 
0

アイコンをツールバーに追加する場合に使用します。

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




<!-- Notification Found --> 
<item android:id="@+id/action_notify_found" 
    android:icon="@drawable/about" 
    android:title="About" 
    app:showAsAction="ifRoom" 
    /> 

<!-- Notification Found --> 
<item android:id="@+id/action_notify_found1" 
    android:icon="@drawable/street_view_icon" 
    android:title="StreetView" 
    app:showAsAction="ifRoom" 
    /> 

Menu.xml

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    this.menu = menu; 
    getMenuInflater().inflate(R.menu.menu_dashboard, menu); 
    menu.getItem(0).setIcon(getResources().getDrawable(R.drawable.ic_about)); 
    menu.getItem(1).setIcon(getResources().getDrawable(R.drawable.street_view_icon)); 


    return true; 
} 



//Notification Icon 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // toggle nav drawer on selecting action bar app icon/title 

    // Handle action bar actions click 
    switch (item.getItemId()) { 


     case R.id.action_notify_found: 
     Intent aboutIntent=new Intent(DashboardActivity.this, HomeAboutActivity.class); 
     startActivity(aboutIntent); 


     case R.id.action_notify_found1: 

     Intent ARStreetviewintent = new Intent(DashboardActivity.this, ARStreetviewActivity.class); 
     startActivity(new Intent(DashboardActivity.this, StreetviewActivity.class)); 

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

。これらのメソッドをオーバーライドして、プロジェクト内のコードを使用します

関連する問題