【问题标题】:How to show a circular logo in action bar in Android?如何在 Android 的操作栏中显示圆形徽标?
【发布时间】:2015-01-27 09:06:14
【问题描述】:

就像上面 Google Plus 的截图一样,我想在操作栏中显示一个圆形头像图像。我该怎么办?

【问题讨论】:

  • link
  • @MD 谢谢,但您不能向 STFW 询问所有问题。我以任何方式搜索了几个关键字,但徒劳无功。
  • 想要工具栏解决方案的人看看我的回答stackoverflow.com/a/67459738/10390808

标签: android android-actionbar


【解决方案1】:

您可以使用位图以编程方式执行此操作:

首先在您的活动的 onCreate() 上执行此操作:

getSupportActionBar().setDisplayShowHomeEnabled(true);
Drawable drawable = new BitmapDrawable(getResources(), createCircleBitmap(sourceBitmap));
getSupportActionBar().setIcon(drawable);

这里是 createCircleBitmap() 方法:

public Bitmap createCircleBitmap(Bitmap bitmapimg){
        Bitmap output = Bitmap.createBitmap(bitmapimg.getWidth(),
                bitmapimg.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmapimg.getWidth(),
                bitmapimg.getHeight());

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawCircle(bitmapimg.getWidth() / 2,
                bitmapimg.getHeight() / 2, bitmapimg.getWidth() / 2, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmapimg, rect, rect, paint);
        return output;
    }

PS:如果你没有Bitmap,你可以用它把Drawable转成Bitmap:

Bitmap sourceBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.your_drawable);

【讨论】:

  • 谢谢,很好的答案,它奏效了。你知道如何让它可点击吗?
【解决方案2】:
  1. 创建圆形图像视图。
  2. 使用圆形图像视图为操作栏创建自定义视图
  3. 膨胀视图并将自定义视图显示为操作栏。
<RelativeLayout         xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/black_pattern" >

<TextView
 android:id="@+id/title_text"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerInParent="true"
 android:textAllCaps="true"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:textColor="#fff"
 android:textStyle="bold" />

<com.example.CircularImageView
 android:id="@+id/circularimageView1"
 android:layout_width="35dp"
 android:layout_height="35dp"
 android:layout_alignParentLeft="true"
 android:layout_centerVertical="true"
 android:layout_marginLeft="8dp"/>

</RelativeLayout> 

Activity.java

ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);

View mCustomView = mInflater.inflate(R.layout.actionbar, null);

mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);

【讨论】:

  • 这是一个解决方案。我在 Android 5.0 中将 Toolbar 用作操作栏,并尝试通过代码创建圆形图像的可绘制对象,并使用 getSupportActionBar().setLogo() 将其放在操作栏上。非常感谢
  • 对于androidx,请使用以下代码:- ActionBar mActionBar = getSupportActionBar(); mActionBar.setDisplayShowHomeEnabled(false); mActionBar.setDisplayShowTitleEnabled(false); LayoutInflater mInflater = LayoutInflater.from(this);查看 mCustomView = mInflater.inflate(R.layout.actionbar, null); mActionBar.setCustomView(mCustomView); mActionBar.setDisplayShowCustomEnabled(true);
【解决方案3】:

当你开始创建项目时(你需要遵循下面的第三步)

Step 1: New Android Project 

Step 2 : Configure Your Project 

第 3 步:配置启动器图标

  You can Choose a shape as Circle 

我希望你需要这个答案

【讨论】:

  • 这不是我想要的。我想在那里显示用户的头像而不是应用程序的图标
  • 哦对不起。让我试试
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-08
  • 2014-01-11
相关资源
最近更新 更多