java - 设置图像时操作栏标题消失
问题描述
我正在尝试将 Image 设置为操作栏,我正在使用 CircularImagView 来做到这一点。
(ScaleType.FIT_END 不适用于 CircularImageView 但我也尝试过 ImageView 相同的结果,即使应用了 ScaleType.FIT_END)。
它适用于更大屏幕的手机,但对于不太宽的手机,标题会消失。
例如标题对于像 nexas 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 中,为了让事情变得不那么复杂,我没有包括它。
这可能是什么解决方案?
解决方案
根据 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 中没有图像视图,因此无法获取参数。希望这可以帮助某人。
推荐阅读
- reactjs - React & Typescript:省略带有扩展语法的道具会导致打字稿错误。高级类型
- arrays - LeetCode TwoSum question returning buggy answer (C Implementation)
- wordpress - Wordpress 在另一个函数中从 wp_signon 获取登录错误
- spring-cloud-contract - 在spring cloud contrat maven插件上指定版本
- javascript - 用 HTML 表单中的数据填充 Excel 电子表格单元格
- python - 为什么我的 import scapy.all 根本不起作用?
- javascript - 在状态发生突变之前调用 SetState 回调
- perl - Intellij Perl5 添加 Perl5 解释器
- android - 棋子无法使用本机反应,但用户界面正确显示?
- angular - 如何在角度 10 中从组件获取 onclick 值到其他组件