我周围中搜索在谷歌和整个这个网站,我发现与我的类似问题,其中如何包含在一个图像传来TextView的文字,例如"我的名字是[图片]",得到的回答是这样的:
ImageSpan is = new ImageSpan(context, resId);
text.setSpan(is, index, index + strLength, 0);
Run Code Online (Sandbox Code Playgroud)
我想在这段代码中知道,
text.setSpan()类似的导入或引用或保留文本吗?如果有人可以为我打破这一点,我将非常感激.
我想在溢出下拉菜单中显示菜单项标题以外的图标.
有可能吗?
我已经看到在android-P google中添加了包含材料芯片的新材料组件库:
所以我决定在我的项目中添加材料输入芯片,但遗憾的是没有找到任何教程如何制作.我想创建类似Gmail芯片的东西,但一开始没有图像.
因为我使用的程序兼容性库我试图通过使用材料的芯片android.support.design.chip.Chip和android.support.design.chip.ChipGroup.但结果只是芯片没有任何输入字段.此外,我尝试创建一个独立的ChipDrawable,然后将其添加到EditText使用
Editable text = editText.getText();
text.setSpan(span, 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Run Code Online (Sandbox Code Playgroud)
但是我没有任何芯片就得到了空的EditText.那么我如何使用这个材料组件库创建像Gmail一样的芯片输入呢?也许有人有经验或知道任何教程,我可以看到如何创建这个?
提前致谢!
目前,我希望在文本之间添加一个图像,并将其对齐到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) 我想将我ImageSpan的文本基线对齐,但我还需要在行之间添加一些间距.
问题是,当我添加行间距时,ImageSpan它不会与文本的基线对齐,而是与文本的基线对齐baseline+lineSpacing,因此它看起来比它应该低.
这有解决方法吗?
编辑:进一步说明:
没有 它的样子lineSpacing(箭头是ImageSpan).它与基线正确对齐.

如果我添加,它看起来如何 android:lineSpacingMulitiplier="1.2"
编辑2代码:
XML:
<com.kushtrim.example.views.CustomTypefaceTextView
android:id="@+id/descId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="3"
android:gravity="center_vertical"
android:layout_marginLeft="@dimen/_45"
android:layout_marginTop="@dimen/_6"
android:layout_marginBottom="@dimen/_20"
app:font_type="merriweather_regular"
android:textSize="@dimen/f40"
android:lineSpacingMultiplier="1.2"
android:textColor="@color/black"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit."
android:ellipsize="end" />
Run Code Online (Sandbox Code Playgroud)
其他相关方法:
private Spannable getContentText(ContactRaport contactRaport) {
DateTime dateTime = new DateTime(contactRaport.contactDate);
String datePart = dateTime.getDayOfMonth() + " " + dateTime.monthOfYear().getAsShortText(new Locale("nl")) + "; ";
String completeText = datePart + contactRaport.note;
Typeface font1 = Typeface.createFromAsset(context.getAssets(), "MyFont1.ttf"); …Run Code Online (Sandbox Code Playgroud) 我在xml布局中具有以下内容:
注意六角形#4如何不在文本的中心对齐。我该怎么做:这是到目前为止我尝试过的事情:
为了真正获得其中带有#的视图,我给视图添加一个如下所示的视图:
//my_hexagon_button.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="0dp"
tools:ignore="MissingPrefix">
<Button
android:id="@+id/tv_icon"
fontPath="proxima_nova_semi_bold.otf"
android:layout_width="16dp"
android:layout_height="17.5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:alpha=".25"
android:background="@drawable/hexagon"
android:clickable="true"
android:contentDescription="@string/content_description"
android:focusable="false"
android:padding="0dp"
android:text="4"
android:textColor="@color/white"
android:textSize="8dp"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
扩大视图后,我获取其图形缓存的副本,并在ImageSpan中使用它。这是我如何获取图形缓存的副本:
public Bitmap getIconBitmap() {
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout myRoot = new LinearLayout(getActivity());
// inflate and measure the button then grab its image from the view cache
ViewGroup parent = (ViewGroup) inflater.inflate(R.layout.my_hexagon_button, myRoot);
TextView tv = (TextView) parent.findViewById(R.id.tv_icon);
parent.setDrawingCacheEnabled(true);
parent.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
parent.layout(0, 0, …Run Code Online (Sandbox Code Playgroud)