那么,有谁知道如何创建下面的网格吗?我需要为每个单词设置clickable事件:

如果四个单词不适合一行,则应在一行中显示三个:

如果它将超过一行n个单词,那么应该在一行中显示n个单词。有谁知道如何实现这一点吗?
发布于 2015-11-19 07:22:44
您可以使用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"/>这是我从这个库中得到的:

您还可以修改该库,使最后一行保持不对齐。文本中的每个单词仍然可以点击。
发布于 2015-11-24 11:15:06
如果您必须添加的项目数量很少,请考虑使用FlowLayout。它扩展了一个LinearLayout,所以只需将它包装在一个ScrollView中,动态地向其中添加视图,就可以了。
https://stackoverflow.com/questions/33651539
复制相似问题