简体   繁体   中英

Dynamic layout for textview - android

I am getting number of string from server.

I want to show it on layout like below image 在此处输入图片说明

Everytime I'll be getting different type of string. so how to create dynamic layout for textview?

May be you are looking for a Flow Layout .

https://github.com/ApmeM/android-flowlayout

using this code, we can able to create linear layout dynamically with inside textview. if you want, just modify the code to as you like, above asked..

 onCreate() Method : 

private Button[] btnAdd ,btnQuestionPalete ; // GLobally
private LinearLayout layout_button ;

layout_button = (LinearLayout) findViewById(R.id.button_layout);

btnAdd = new Button[100] ;

 LayoutParams param2 = new LinearLayout.LayoutParams(
 LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f);

    btnQuestionPalete = new Button[100];
    for (int i = 0; i < noOfButton; i++) {

    linearQuestionPalettesRow = new LinearLayout(getApplicationContext());
    linearQuestionPalettesRow.setOrientation(LinearLayout.VERTICAL);
    linearQuestionPalettesRow.setLayoutParams(param2);
    linearQuestionPalettesRow.setPadding(10, 10, 10, 10);

            textView[i] = new TextView(getApplicationContext());
            textView[i].setId(i+1);
            textView[i].setText("test");
            textView[i].setTextColor(Color.BLACK);
            textView[i].setGravity(Gravity.CENTER_HORIZONTAL);
            linearQuestionPalettesRow.addView(textView[i]);
            layout_button.addView(linearQuestionPalettesRow);
        }


xml code :
<LinearLayout
        android:id="@+id/button_layout"
        android:layout_width="wrap_content"
        android:layout_height="75dp"
        android:orientation="horizontal"
         >



        </LinearLayout>

只需将字符串添加到array / arraylist并使之成为arrayadapter即可 ,然后将listViewDridView传递给它很容易

You can use Table Layout to achieve this dynamically. but i think you want your layout in zigzag manner like:

------------------
  ------------------
------------------
  ------------------

so you can do it using ListView (efficient) but if you don't want this pattern and you don't have rows more than 5-10 then you can go for TableLayout

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