简体   繁体   中英

Action Bar Title Disappearing When Setting Image

I am trying to set Image into action bar, I am using CircularImagView to do that.

(ScaleType.FIT_END does not work for CircularImageView but i have also tried ImageView Same result even when ScaleType.FIT_END is applied).

It works fine for a larger screen phone but the title disappears for a less wide phone.

for example title shows fine for a wide phone like nexas 5x

连结5倍

But does not show up in a narrow device like my physical phone.

并排比较

here is the code i am using to set the image

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

Even the ScaleType.FIT_END or Gravity.RIGHT does not help

(ScaleType.FIT_END does not work for CircularImageView but i have also tried ImageView Same result even when ScaleType.FIT_END is applied).

bitmap is global variable and i am setting it in onCreate, to make things less complicated i didn't included that.

What might be the solution for this ?

By the suggestion of Manohar Reddy I have Created a LinearLayout to create a LayoutParam, have set width and height on that and have set that layout param to the imageview which fixed the issue

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

had to create a new layout param cause there no imageview in activity xml so can't get the params. Hope this helps someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM