首页 > 解决方案 > 设置图像时操作栏标题消失

问题描述

我正在尝试将 Image 设置为操作栏,我正在使用 CircularImagView 来做到这一点。

(ScaleType.FIT_END 不适用于 CircularImageView 但我也尝试过 ImageView 相同的结果,即使应用了 ScaleType.FIT_END)。

它适用于更大屏幕的手机,但对于不太宽的手机,标题会消失。

例如标题对于像 nexas 5x 这样的宽手机显示很好

耐克萨斯 5x

但不会出现在像我的实体手机这样的狭窄设备中。

并排比较

这是我用来设置图像的代码

             try {
                // setting the image in the actionbar
                getSupportActionBar().setDisplayOptions(getSupportActionBar().getDisplayOptions()
                        | ActionBar.DISPLAY_SHOW_CUSTOM);
                CircleImageView imageView = new CircleImageView(getSupportActionBar().getThemedContext());
                //imageView.setScaleType(CircleImageView.ScaleType.FIT_END);
                //imageView.setForegroundGravity(Gravity.RIGHT);
                imageView.setImageBitmap(bitmap);
                getSupportActionBar().setCustomView(imageView);
                // setting the image in actionbar ends here
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            }

即使是 ScaleType.FIT_END 或 Gravity.RIGHT 也无济于事

(ScaleType.FIT_END 不适用于 CircularImageView 但我也尝试过 ImageView 相同的结果,即使应用了 ScaleType.FIT_END)。

位图是全局变量,我将它设置在 onCreate 中,为了让事情变得不那么复杂,我没有包括它。

这可能是什么解决方案?

标签: javaandroidandroid-actionbarandroid-imageview

解决方案


根据 Manohar Reddy 的建议,我创建了一个 LinearLayout 来创建一个 LayoutParam,并在其上设置了宽度和高度,并将该布局参数设置为解决问题的 imageview

    try {
                    // setting the image in the actionbar
                    getSupportActionBar().setDisplayOptions(getSupportActionBar().getDisplayOptions()
                            | ActionBar.DISPLAY_SHOW_CUSTOM);
                    CircleImageView imageView = new CircleImageView(getSupportActionBar().getThemedContext());

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(70, 70);
                    imageView.setLayoutParams(layoutParams);
                    imageView.setImageBitmap(bitmap);
                    getSupportActionBar().setCustomView(imageView);
                    // setting the image in actionbar ends here
     } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
                }

必须创建一个新的布局参数,因为活动 xml 中没有图像视图,因此无法获取参数。希望这可以帮助某人。


推荐阅读