菜单图标和标题图标显示不起作用

时间:2016-07-15 21:23:36

标签: java android xml

我想设置一个菜单图标(关于应用程序)到我的标题的RHS,还有一个图标(如后退按钮)到我标题的LHS。 android:icon似乎不适用于我的清单活动xml。

1 个答案:

答案 0 :(得分:0)

     For Back Button:
        1)Without using Toolbar
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

       2)With Toolbar
       Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar_gas_station);
       setSupportActionBar(toolbar);
       getSupportActionBar().setDisplayHomeAsUpEnabled(true);


     For Icon:
        1)Without using Toolbar
         You have to make menu file for this and then Override onOptionsItemSelected and onCreateOptionsMenu then write your code accordingly

       2)With Toolbar:

        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            android:fitsSystemWindows="true">
   <android.support.v7.widget.Toolbar
      android:id="@+id/toolbar_gas_station"
       android:layout_width="match_parent"
       android:layout_height="?attr/actionBarSize"
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light"  >
    <LinearLayout
        android:id="@id/ll_header_find_parking_map"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:gravity="right"
        android:padding="@dimen/fifteen">
        <ImageView
            android:id="@+id/iv_search_find_parking_map"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@drawable/search"/>
    </LinearLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

  You can handle the click event as we do in normal ImageView.