それで、次のグリッドを作成する方法を知っている人はいますか? クリック可能なイベントをすべての単語に設定する必要があります。
4 つの単語が 1 行に収まらない場合は、3 つ並べて表示する必要があります。
1 行に n ワードを超える場合は、1 行に n ワードを表示する必要があります。これを実装する方法を知っている人はいますか?
それで、次のグリッドを作成する方法を知っている人はいますか? クリック可能なイベントをすべての単語に設定する必要があります。
4 つの単語が 1 行に収まらない場合は、3 つ並べて表示する必要があります。
1 行に n ワードを超える場合は、1 行に n ワードを表示する必要があります。これを実装する方法を知っている人はいますか?
SpannableString と ClickableSpan を使用できます。たとえば、このアクティビティは、テキストで TextView を作成し、各単語のクリックを管理します。
public class MainActivity extends AppCompatActivity {
Activity activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity = this;
TextView textView = (TextView) findViewById(R.id.textView);
String text = "up down Antidisestablishment over took dropped lighten with from throught fell on up down Antidisestablishment over took dropped lighten with from throught fell on";
String[] textArray = text.split(" ");
SpannableString ss = new SpannableString(text);
int start = 0;
int end = 0;
for(final String item : textArray){
end = start + item.length();
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
Toast.makeText(activity, "Say " + item+ "!", Toast.LENGTH_SHORT).show();
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
};
ss.setSpan(clickableSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
start += item.length()+1;
}
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.TRANSPARENT);
}
}
ここに activity_main.xml があります:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:padding="5dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textColor="#000000"
android:textColorLink="#000000"/>
</LinearLayout>
任意の単語をクリックすると、この単語でポップアップが表示されます
編集: テキストを正当化するには、ライブラリandroid-justifiedtextview を使用します
しかし、gradle からのライブラリではなく、サポートしていない古いバージョンがありSpannableString
ます。クラスJustifyTextView
を git からプロジェクトにコピーすることをお勧めします。.xml
次に、このビューを好きなように使用できます。
<com.yourdomain.yourproject.JustifyTextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textColor="#000000"
android:textColorLink="#000000"/>
ライブラリを変更して、最後の行を正当化しないようにすることもできます。テキスト内のすべての単語は引き続きクリック可能です。
追加する項目の量が少ない場合は、FlowLayoutの使用を検討してください。これは LinearLayout を拡張するので、 でラップし、ScrollView
ビューを動的に追加するだけで準備完了です。