目前,我希望在文本之间添加一个图像,并将其对齐到TextView的顶部.
像这样的东西:
我能找到的唯一垂直对齐是基线(似乎将它放在文本的中心)并对齐底部.
如果我使用ALIGN_BASELINE会发生什么:
有没有办法将它与顶部对齐?
我目前的代码:
txtView.setText(this.addImageAsterisk(
"The string to have asterisk at the end*"), BufferType.SPANNABLE);
Run Code Online (Sandbox Code Playgroud)
然后
private CharSequence addImageAsterisk(String string) {
Drawable d = context.getResources().getDrawable(R.drawable.img_asterisk);
ImageSpan imageSpan = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
final SpannableString spannableString = new SpannableString(string);
spannableString.setSpan(imageSpan, string.length()-1, string.length(), 0);
return spannableString;
}
Run Code Online (Sandbox Code Playgroud)
删除ImageSpan.ALIGN_BASELINE将其设置为与底部对齐,这也不是我预期的结果.
---感谢用户Lalit Poptani,我尝试应用你的答案----应用之后,会发生的事情是整个textview似乎有额外的余量.
在应用范围之前:
This is the text*
Run Code Online (Sandbox Code Playgroud)
在应用SuperscriptSpanAdjuster之后
(some extra space)
This is the text*
Run Code Online (Sandbox Code Playgroud)
我的代码:
String string = "This is the text*";
Drawable d = this.context.getResources().getDrawable(R.drawable.img_asterisk);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
ImageSpan imageSpan = …
Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,当连接/断开蓝牙设备时打开警报对话框. 警报对话框由连接蓝牙设备的BroadcastReceiver触发.
我想打开一个警告对话框,如果我打开我的应用程序(应用程序A)>长按主页>转到另一个应用程序(应用程序B),蓝牙设备已连接 - >我的应用程序A 警报将显示在顶部应用B.
现在发生的事情是,如果我回到应用程序A,我只能看到对话框
我目前的代码:
final AlertDialog.Builder dialog = new AlertDialog.Builder(activity,
AlertDialog.THEME_DEVICE_DEFAULT_DARK);
... some setting here
final AlertDialog alert = dialog.create();
alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alert.show();
Run Code Online (Sandbox Code Playgroud)