首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GridView:使用RecyclerView或简单地将TextView与可点击的单词一起使用

GridView:使用RecyclerView或简单地将TextView与可点击的单词一起使用
EN

Stack Overflow用户
提问于 2015-11-11 21:26:36
回答 2查看 333关注 0票数 2

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

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

如果它将超过一行n个单词,那么应该在一行中显示n个单词。有谁知道如何实现这一点吗?

EN

回答 2

Stack Overflow用户

发布于 2015-11-19 07:22:44

您可以使用SpannableString和ClickableSpan。例如,此活动使用您的文本创建TextView,并管理对每个单词的点击:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<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中使用此视图,如下所示:

代码语言:javascript
复制
<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"/>

这是我从这个库中得到的:

您还可以修改该库,使最后一行保持不对齐。文本中的每个单词仍然可以点击。

票数 2
EN

Stack Overflow用户

发布于 2015-11-24 11:15:06

如果您必须添加的项目数量很少,请考虑使用FlowLayout。它扩展了一个LinearLayout,所以只需将它包装在一个ScrollView中,动态地向其中添加视图,就可以了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33651539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档