简体   繁体   中英

how to wrap the content of textview auto resize

I need to set parameters for a textview that is created dynamically, the text of the textview is from database. As some text may be large it exceeds the mobile window and continues to show the text in single line

    TableLayout tbl=(TableLayout) findViewById(R.id.tbla);
    TableRow row= new TableRow(this);
            TableRow.LayoutParams lp = new    TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
            row.setLayoutParams(lp);
TextView tv1 = new TextView(this);
            tv1.setSingleLine(false);
            tv1.setEllipsize(null);
            tv1.setTextSize(getResources().getDimension(R.dimen.textsize));
            tv1.setHorizontallyScrolling(false);
            tv1.setText(log);
            row.addView(tv1);
            tbl.addView(row,i);

I have read some articles and made some possible properties to it.. plz help me to make it auto resize..

Just add this line on your code, before adding your TextView to the TableRow :

tv1.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f));

It will make your TextView to always show all its height.

Try this way,hope this will help you to solve your problem.

TableLayout tbl=(TableLayout) findViewById(R.id.tbla);
TableRow row= new TableRow(this);
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
TextView tv1 = new TextView(this);
tv1.setTextSize(getResources().getDimension(R.dimen.textsize));
tv1.setText(log);
row.addView(tv1,new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tbl.addView(row,i);

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