java - 如何在运行时显示多个图像(换行到下一行)

标签 java android imageview

我有一个相当奇怪的应用程序,我将在运行时获取单词/符号,但不会提前知道它们。我需要显示它们,并在文本太宽而无法显示时自动换行。

我有点不确定我应该在功能上使用什么布局,但任何提示将不胜感激。如果我可以将它们添加到卡片 View 中并让它整理包装,那就太好了。有办法做到这一点吗?

下面的代码应该显示“这是一个在运行时动态组合的句子”。

example output

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v7.widget.CardView
        android:id="@+id/card"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:cardCornerRadius="40dp"
        android:layout_centerHorizontal="true"
        android:innerRadius="0dp"
        android:shape="ring"
        android:thicknessRatio="1.9"
        app:contentPadding="13dp"
        >


            <LinearLayout
                android:id="@+id/layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"

                android:orientation="horizontal">

            </LinearLayout>

    </android.support.v7.widget.CardView>

</android.support.constraint.ConstraintLayout>

MainActivity.java

        LinearLayout ll = (LinearLayout)findViewById(R.id.layout);

        ImageView ivThis = new ImageView(MainActivity.this);
        ivThis.setImageResource(R.drawable.word_this);
        ll.addView(ivThis);

        ImageView ivIs = new ImageView(MainActivity.this);
        ivIs.setImageResource(R.drawable.word_is);
        ll.addView(ivIs);

        ImageView ivA = new ImageView(MainActivity.this);
        ivA.setImageResource(R.drawable.word_a);
        ll.addView(ivA);

        ImageView ivSentance = new ImageView(MainActivity.this);
        ivSentance.setImageResource(R.drawable.word_sentance);
        ll.addView(ivSentance);

        ImageView ivPut = new ImageView(MainActivity.this);
        ivPut.setImageResource(R.drawable.word_put);
        ll.addView(ivPut);

        ImageView ivTogether = new ImageView(MainActivity.this);
        ivTogether.setImageResource(R.drawable.word_together);
        ll.addView(ivTogether);

        ImageView ivDynamically = new ImageView(MainActivity.this);
        ivDynamically.setImageResource(R.drawable.word_dynamically);
        ll.addView(ivDynamically);

        ImageView ivAt = new ImageView(MainActivity.this);
        ivAt.setImageResource(R.drawable.word_at);
        ll.addView(ivAt);

        ImageView ivRuntime = new ImageView(MainActivity.this);
        ivDynamically.setImageResource(R.drawable.word_runtime);
        ll.addView(ivRuntime);

最佳答案

I have a rather strange application where I will get the words/symbols at runtime and wont know them in advance. I need to display them and word wrap the text when it is too wide for the display.

听起来您正在寻找 RecyclerView动态显示数据(在运行时)。

关于java - 如何在运行时显示多个图像(换行到下一行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56614931/

相关文章:

java - Android 中的数组编辑

java - ConcurrentModificationException : . add() 与 .addAll()

java - Android 列表选择器半透明

Android ImageView 使应用变慢

安卓 ImageView

Android - Imageview 在所有屏幕分辨率的中心

java - Spark 集群在更大的输入上失败,适用于小的

java - 如何处理调用commit()或flush()后的异常

Android 相对布局和不同的屏幕尺寸/设备

android - Android模拟器可以访问PC硬盘中的文件吗?