如何在android中的窗口默认操作栏中添加图像?

时间:2016-07-13 09:34:05

标签: android

我有窗口默认操作栏,其中想要添加高度和宽度为30x30的Imageview。我可以这样做,我正在尝试此代码,但无法看到Imageview。

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.amazon);
    ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(30, 30, Gravity.RIGHT | Gravity.CENTER_VERTICAL);
    layoutParams.rightMargin = 40;
    imageView.setLayoutParams(layoutParams);
    actionBar.setCustomView(imageView);

1 个答案:

答案 0 :(得分:0)

替换

ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(30, 30, Gravity.RIGHT | Gravity.CENTER_VERTICAL);

ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
        ActionBar.LayoutParams.WRAP_CONTENT,
        ActionBar.LayoutParams.WRAP_CONTENT, Gravity.RIGHT
                | Gravity.CENTER_VERTICAL);