現在、テキストの一部の背後にシェイプを表示し、次にテキストの次の部分の背後に別のシェイプを表示したいという問題に直面しています。私は複数のTextViewsを使ってみましたが、1つのテキストのように表示する方法を知りませんでした。私もSpannableStringを使ってみましたが、この作業をすることはできませんでした。複数の種類の図形をAndroidのテキストの背後に表示
マイ形状がドロウアブルとしてロードされた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は、私はこの結果に来ることを示唆した:
これは改善ですが、まだ完了していません。私が使用していますフレキシボックスのコードは次のとおりです。
<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>
おかげで背景を設定し、私はあなたが提案しフレキシボックスに見えました。私は完全に私がそれを望む方法を得ることができませんでしたが、それは正しい方向への一歩です。私はOPを編集しました。 – user2988879
問題はどこにあるのかよくわかりません。たぶん、長いテキスト部分を1行に収めるために小さなテキスト部分に分割するには、テキスト幅を測定する必要がありますか? https://stackoverflow.com/a/18958569/6507689 –