简体   繁体   English

如何在操作栏中添加图像?

[英]How to add image to action bar?

  I am using this code to add image to action bar.

ActionBar actionBar = getSupportActionBar(); ActionBar actionBar = getSupportActionBar();

    actionBar.setDisplayOptions(actionBar.getDisplayOptions()
            | ActionBar.DISPLAY_SHOW_CUSTOM);
    ImageView imageView = new ImageView(actionBar.getThemedContext());
    imageView.setScaleType(ImageView.ScaleType.CENTER);
    imageView.setImageResource(R.drawable.logo_actionbar);
    ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
            ActionBar.LayoutParams.WRAP_CONTENT,
            ActionBar.LayoutParams.WRAP_CONTENT, Gravity.RIGHT
                    | Gravity.CENTER_VERTICAL);
    layoutParams.rightMargin = 0;
    imageView.setLayoutParams(layoutParams);
    actionBar.setCustomView(imageView);

But stil I am getting image in centre.Can you please tell me where I am wrong?Thanks in advance. 但是,直到我在中心得到形象。您能告诉我我哪里错了吗?谢谢。

Inflate a LinearLayout or other layout and add the ImageView to the layout. 膨胀LinearLayout或其他布局,然后将ImageView添加到该布局。

The problem here is that the ImageView is only occupying the space required for the image. 这里的问题是ImageView仅占用图像所需的空间。 The LinearLayout can fill the ActionBar with the ImageView on the right. LinearLayout可以用右侧的ImageView填充ActionBar。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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