2016-11-29 23 views
0

我需要将图像添加到导航抽屉工具栏/操作栏。显示在菜单图标右侧和活动标题左侧的小圆形图像。 我还需要将工具栏/操作栏的背景更改为我自己的自定义图像。将图像添加到导航抽屉工具栏并设置自定义背景

这是可能的,如果是的话我该如何在我的android应用程序中完成此操作?

+0

是的,这是可能的,看看工具栏,是否有意义,你想要完成什么。 https://guides.codepath.com/android/Using-the-App-ToolBar https://developer.android.com/reference/android/widget/Toolbar.html – MikeOscarEcho

+0

当我尝试添加自定义工具栏导航抽屉的工具栏覆盖在顶部。 – user3718908

+0

这是一个完全不同的问题寿。你想知道你在OP中提到的事情是否可能,并且我已经告诉你他们是。 试试这个教程。它使用一个带有NavigationView的工具栏,如果你通过我发布的前两个链接,你应该很好。这些是我在不到6个月前帮助过我的资源。 https://guides.codepath.com/android/Fragment-Navigation-Drawer – MikeOscarEcho

回答

1

对于任何困惑的人,OP都在使用Android Studio的NavigationView Activity而不是空行为。

图片的汉堡包菜单图标的右边,在标题的左边是实现这样的:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 

if (getSupportActionBar() != null) { 
    getSupportActionBar().setDisplayShowTitleEnabled(false); // hide built-in Title 

    // Setting background using a drawable 
    Drawable toolbarBackground = getResources().getDrawable(R.drawable.ic_menu_gallery); 
    getSupportActionBar().setBackgroundDrawable(toolbarBackground); 
} 

现在,我们已经隐藏标题,我们要增加我们自己的冠军TextView和图标ImageView到工具栏,这将是您的app_bar_main.xml布局(res /布局)。现在

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    app:popupTheme="@style/AppTheme.PopupOverlay"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <ImageView 
      android:id="@+id/my_custom_icon" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_menu_share"/> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="My Custom Title" 
      android:id="@+id/my_custom_title" 
      android:layout_toRightOf="@id/menu_icon"/> 
    </RelativeLayout> 
</android.support.v7.widget.Toolbar> 

,你可以参考my_custom_title和my_custom_icon,并添加你想要的(你可以有布局鼓捣,我只是指出你在正确的方向),无论标题和图标。

TextView myCustomTitleTV = (TextView) findViewById(R.id.my_custom_title); 
myCustomTitleTV.setText("My Custom Title"); 
+0

谢谢,这似乎工作! :) – user3718908

相关问题