如何在运行时显示多个图像(换行到下一行)
How To Display Multiple Images At Runtime (Wraping to the next line)
我有一个相当奇怪的应用程序,我会在运行时获得 words/symbols,但不会提前知道它们。我需要显示它们并在文本太宽无法显示时自动换行。
我有点不确定在功能上我应该使用什么布局,但如果有任何提示,我们将不胜感激。如果我可以将它们添加到卡片视图并让它整理包装,那就太好了。有办法吗?
下面的代码应该显示 "This is a sentence put together dynamically at runtime"。
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 来动态显示数据(在运行时)。
希望这可以帮助其他人。以下是我如何获得我正在寻找的功能。
Gradle
dependencies {
implementation 'com.google.android:flexbox:1.1.0'
}
XML
<com.google.android.flexbox.FlexboxLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:flexWrap="wrap"
app:alignItems="stretch"
app:alignContent="stretch" >
</com.google.android.flexbox.FlexboxLayout>
我有一个相当奇怪的应用程序,我会在运行时获得 words/symbols,但不会提前知道它们。我需要显示它们并在文本太宽无法显示时自动换行。
我有点不确定在功能上我应该使用什么布局,但如果有任何提示,我们将不胜感激。如果我可以将它们添加到卡片视图并让它整理包装,那就太好了。有办法吗?
下面的代码应该显示 "This is a sentence put together dynamically at runtime"。
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 来动态显示数据(在运行时)。
希望这可以帮助其他人。以下是我如何获得我正在寻找的功能。
Gradle
dependencies {
implementation 'com.google.android:flexbox:1.1.0'
}
XML
<com.google.android.flexbox.FlexboxLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:flexWrap="wrap"
app:alignItems="stretch"
app:alignContent="stretch" >
</com.google.android.flexbox.FlexboxLayout>