2017-08-16 52 views
2

我想在注销图标之前在操作栏上显示个人资料图片,但在App标题之后显示图片。我已经使用Image View和Picasso从服务器使用volley加载图像。如何实现这一目标?如何在右侧边缘(即)在注销图标之前显示图像视图?

actionBar.setDisplayOptions(actionBar.getDisplayOptions() 
          | ActionBar.DISPLAY_SHOW_CUSTOM); 

        imageView.setScaleType(ImageView.ScaleType.CENTER); 
        Picasso.with(HomeActivity.this).load(AppConfig.profilePic + image).placeholder(R.drawable.profile).transform(new CircleTransform()).fit().into(imageView); 
        ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
          ActionBar.LayoutParams.WRAP_CONTENT, 
          ActionBar.LayoutParams.WRAP_CONTENT, Gravity.RIGHT 
          | Gravity.CENTER_VERTICAL); 
        layoutParams.gravity = Gravity.RIGHT; 
        imageView.setLayoutParams(layoutParams); 
        actionBar.setCustomView(imageView); 

enter image description here

+0

你必须实现自定义工具栏,而不是动作条 –

+0

实现与android.support.v7.widget.Toolbar自定义工具栏与相对布局内的ImageView和TextView的容器 –

回答

3

这不能在那里进行作为其标准显示标题第一 解决方案>>使用自定义的操作栏 1 - 构建您自己的布局 2-将样式文件中的应用更改为NoAction栏 3-使用include将您的自定义动作条添加到任何您想要的布局t

<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
    app:layout_scrollFlags="scroll|enterAlways" 
    app:layout_collapseMode="pin"> 
    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <ImageView 
      android:id="@+id/toolbar_logo" 
      android:src="@drawable/logo" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_marginRight="?attr/actionBarSize" 
      android:layout_marginTop="4dp" 
      android:layout_marginBottom="4dp" /> 

     <TextView 
      android:id="@+id/toolbar_title" 
      android:orientation="horizontal" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_marginRight="?attr/actionBarSize" 
      android:layout_gravity="center" 
      android:gravity="center_vertical" 
      android:visibility="gone" 
      android:text="@string/app_name" 
      android:textColor="@color/white" 
      style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse" 
      /> 


    </FrameLayout> 
</android.support.v7.widget.Toolbar> 
2

您要创建自定义工具栏像下面

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/toolbarLogo" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="@color/colorPrimary" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <ImageView 
      android:id="@+id/ivBackArrow" 
      android:layout_width="32dp" 
      android:layout_height="32dp" 
      android:layout_centerVertical="true" 
      android:background="@drawable/ic_back_arrow_black" /> 

     <TextView 
      android:id="@+id/toolbar_text_title" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_gravity="center" 
      android:gravity="left|center_vertical" 
      android:singleLine="true" 
      android:layout_toRightOf="@+id/ivBackArrow" 
      android:layout_marginLeft="4dp" 
      android:layout_toLeftOf="@+id/iv_profile" 
      android:layout_centerHorizontal="true" 
      android:text="ALERT" 
      android:textColor="@color/colorBlack" 
      android:textSize="20sp" /> 

     <ImageView 
      android:id="@+id/iv_profile" 
      android:layout_width="32dp" 
      android:layout_height="32dp" 
      android:layout_centerVertical="true" 
      android:layout_toLeftOf="@+id/iv_logout" 
      android:background="@drawable/ic_back_arrow_black" /> 

     <ImageView 
      android:id="@+id/iv_logout" 
      android:layout_width="32dp" 
      android:layout_height="32dp" 
      android:layout_centerVertical="true" 
      android:layout_alignParentRight="true" 
      android:background="@drawable/ic_back_arrow_black" /> 
    </RelativeLayout> 

</android.support.v7.widget.Toolbar> 
0

您可以将图像设置为应用程序徽标。试试下面的代码

Picasso.with(this).load(yourImageUrl).into(new Target() 
    { 
     @Override 
     public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) 
     { 
      Drawable d = new BitmapDrawable(getResources(), bitmap); 
      ab.setIcon(d); 
      ab.setDisplayShowHomeEnabled(true); 
      ab.setDisplayHomeAsUpEnabled(true); 
     } 

     @Override 
     public void onBitmapFailed(Drawable errorDrawable) 
     { 
     } 

     @Override 
     public void onPrepareLoad(Drawable placeHolderDrawable) 
     { 
     } 
    }); 
相关问题