操作栏标题在设置图像时消失

问题描述 投票:1回答:1

我正在尝试将Image设置为操作栏,我正在使用CircularImagView来执行此操作。

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

它对于较大的屏幕电话工作正常,但对于不太宽的电话,标题消失。

例如,标题对于像nexus 5x这样的宽手机来说很好

nexas 5x

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

side by side comparison

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

             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,但即使应用了ScaleType.FIT_END,我也尝试了ImageView相同的结果)。

位图是全局变量,我在onCreate中设置它,使事情变得不那么复杂,我没有包含它。

这可能是什么解决方案?

java android android-actionbar android-imageview
1个回答
0
投票

根据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中没有imageview,因此无法获得参数。希望这有助于某人。

© www.soinside.com 2019 - 2024. All rights reserved.