【问题标题】:How to show the imageview in right hand side edge(i.e) before the logout icon?如何在注销图标之前在右侧边缘(即)显示图像视图?
【发布时间】:2017-08-16 06:30:26
【问题描述】:

我想在注销图标之前的操作栏上显示个人资料图片,但在应用程序标题之后显示图片。我已经使用 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);

【问题讨论】:

  • 你必须实现自定义工具栏而不是 ActionBar
  • 使用android.support.v7.widget.Toolbar实现自定义工具栏,ImageView和TextView中的Relative layout容器

标签: android android-actionbar imageview action


【解决方案1】:

这不能作为首先显示标题的标准 解决方案>>使用自定义操作栏 1-建立自己的布局 2-在样式文件中将您的应用程序的主题更改为 NoAction 栏 3- 使用 include 将您的自定义 actionBar 添加到您想要的任何布局中

<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>
    

    【讨论】:

      【解决方案3】:

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

      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)
             {
             }
         });
      

      【讨论】:

        猜你喜欢
        • 2014-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-06
        • 1970-01-01
        • 2019-05-29
        • 1970-01-01
        • 2013-10-24
        相关资源
        最近更新 更多