【发布时间】:2017-11-11 20:48:01
【问题描述】:
我目前面临一个问题,我想在文本的一部分后面显示一个形状,然后在下一部分文本后面显示一个不同的形状。我尝试使用多个 TextView,但我不知道如何让它们像一个文本一样显示。我也尝试使用 SpannableString,但我无法完成这项工作。为了更清楚地说明这一点,这就是我想要的方式:
我的形状是 XML 文件,加载为可绘制对象。
我试过的 SpannableString 代码是这样的:
SpannableString ss = new SpannableString("abc");
Drawable d1 = getResources().getDrawable(R.drawable.oval_background);
d1.setBounds(0, 0, d1.getIntrinsicWidth(), d1.getIntrinsicHeight());
ImageSpan span = new ImageSpan(d1, ImageSpan.ALIGN_BASELINE);
ss.setSpan(span, 0, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
tv.setText(ss);
但文字和背景都没有出现在这里。尝试另一个可绘制对象会显示可绘制对象,但不会显示文本。
这是oval_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:bottomLeftRadius="30dp"
android:bottomRightRadius="30dp"
android:radius="60dp"
android:topLeftRadius="30dp"
android:topRightRadius="30dp" />
<solid android:color="#1287A8" />
<padding
android:bottom="5dp"
android:left="10dp"
android:right="10dp"
android:top="5dp" />
</shape>
现在我的问题是,获得我想要的结果的最佳方法是什么?
编辑:将 Flexbox 用作 @R。 Zagórski 建议,我得出这个结果:
这是一项改进,但尚未完成。我使用的 Flexbox 代码是:
<com.google.android.flexbox.FlexboxLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:flexDirection="row"
app:flexWrap="wrap"
app:justifyContent="flex_start"
app:alignItems="flex_start"
app:alignContent="flex_start"
android:id="@+id/flexbox"
android:layout_marginTop="@dimen/text_margin_small"
android:layout_marginLeft="@dimen/text_margin_small"
android:layout_marginRight="@dimen/text_margin_small">
</com.google.android.flexbox.FlexboxLayout>
【问题讨论】:
标签: java android xml android-layout