gpt4 book ai didi

android - TextDrawable - getSupportActionBar.setLogo(Drawable drawable) 不起作用

转载 作者:行者123 更新时间:2023-11-29 20:11:53 25 4
gpt4 key购买 nike

我想在我的工具栏中放置一个带有来自 https://github.com/amulyakhare/TextDrawable 的自定义可绘制对象的 Logo .

但是这段代码什么也没显示

TextDrawable drawable = TextDrawable.builder()
.buildRound("A", Color.RED);
getSupportActionBar().setLogo(drawable);

但如果我尝试使用“普通”可绘制对象,它会起作用。

getSupportActionBar().setLogo(R.drawable.ic_launcher);

提前致谢

最佳答案

编辑: Mike M. 在评论中添加的解决方案效果不错,但看起来很糟糕:

下面是这个解决方案的代码:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextDrawable drawable = TextDrawable.builder().beginConfig().width(40).height(40).endConfig()
.buildRound("A", Color.RED);
toolbar.setLogo(drawable);

NOTE: In this solution you need to set width() and height() of your TextDrawable logo, as it has as default value -1. Without that you won't see your TextDrawable icon.

That's because of that the Toolbar class creates logo dynamically with wrap_content parameters.

TextDrawable takes width and height of your ImageView, so please DON'T use wrap_content value, otherwise it would get default -1 value and you won't see your image.

Instead of it, set hard-coded value like in an example below, match_parent or use layout_weight to set how big your TextDrawable you want.

这是我的解决方案 - 使用自定义工具栏创建一个 TextDrawable Logo

  1. 创建名称为 action_bar.xml 的自定义布局
  2. 将这段代码放入其中

    <ImageView
    android:id="@+id/image_view"
    android:layout_width="32dp"
    android:layout_height="32dp"
    tools:src="@mipmap/ic_launcher"/>

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:text="@string/app_name"
    android:textColor="#ffffff"
    android:textSize="24sp"
    android:textAlignment="gravity"
    android:gravity="center_vertical"/>

  3. 将此代码添加到 MainActivity 类中的 onCreate 方法:

    //SET A DRAWABLE TO IMAGEVIEW
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(R.layout.actionbar_main);

    TextDrawable drawable = TextDrawable.builder()
    .buildRound("A", getResources().getColor(R.color.colorAccent));
    ImageView imageView = (ImageView) findViewById(R.id.image_view);
    imageView.setImageDrawable(drawable);
  4. 修改后应该是这样的:

希望对你有帮助

关于android - TextDrawable - getSupportActionBar.setLogo(Drawable drawable) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34650775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com