简体   繁体   中英

Inflating multiple TextViews on a horizontal LinearLayout

for(int i=0;i<45;i++){
                    textView[i]=new TextView(context);
                    iInflateGoesHere.addView(stringArray[i]);//iInflateGoesHere is a horizontal LinearLayout
                    textView[i].setText(" "+i);

                }

the result is : so it goes to the end of the line , tries to fit "22" and you can see the result on the screenshot.

of course, showing numbers is not my aim, I want to add strings from array so it will look like normal text - when one line ends, text should go from the start of next line.

How can I acheive that - so number 22 will go from the start of line 2 ?

在此输入图像描述

First of all, your attitude towards forum users is very annoying. Sentences like "I knew you wouldn't solve my question" or "please read my question!" should be avoided, as here you can find people trying to help you, not people paid to give you services.

Saying that, I think the only way to solve your issue is to programmatically get the width of your screen and evaluate how many TextViews can fit on the screen, then using nested LinearLayout (the outer vertical and the inner horizontal)

如果您仍在寻找解决方案,请考虑这可能会有所帮助https://github.com/ApmeM/android-flowlayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <TextView
        android:id="@+id/mylayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="asdfffffffdfaffdffffsffdsffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" >
    </TextView>

</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM