如何修复TextView太大?

时间:2018-09-07 07:18:38

标签: java android

我正在使用带有sqlite数据库的项目,但似乎无法解决此问题。我该如何解决? 这是我的代码:

 TableRow.LayoutParams trs= new 
 TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
 TableRow.LayoutParams.WRAP_CONTENT);
 trs.setMargins(0,0,0,0);

    for (int i = 0; i < id.length; i++)

    {

        /** Create a TableRow dynamically **/

        tr = new TableRow(this);

        tr.setLayoutParams(new TableRow.LayoutParams(

                TableRow.LayoutParams.FILL_PARENT,

                TableRow.LayoutParams.WRAP_CONTENT));

        /** Creating a TextView to add to the row **/

        idtxt = new TextView(this);
        AutofitHelper.create(idtxt);
        idtxt.setBackgroundColor(Color.parseColor("#00ff00"));
        idtxt.setText(id[i]);
        idtxt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if(idtxt.getLineCount() > 1){
                    idtxt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                }
            }
        });


        idtxt.setTextColor(Color.RED);
        idtxt.setWidth(-100);
        idtxt.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

        idtxt.setLayoutParams(trs);
      //  idtxt.setPadding(0, 0, 0, 5);
        idtxt.setBackgroundColor(new Color(Color.green()));

        tr.addView(idtxt);  // Adding textView to tablerow.
        tl.addView(tr, new TableLayout.LayoutParams(

                TableRow.LayoutParams.MATCH_PARENT,

                TableRow.LayoutParams.WRAP_CONTENT));
        // Add the TableRow to the TableLayout

        /** Creating a TextView to add to the row **/

        timestamptxt = new TextView(this);

        timestamptxt.setText(Timestamp[i]);

        timestamptxt.setTextColor(Color.RED);

        timestamptxt.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

        timestamptxt.setLayoutParams(trs);
        timestamptxt.setPadding(-5, -5, -5, -5);
        tr.addView(timestamptxt);
        tl.removeView(tr);
        tl.addView(tr, new TableLayout.LayoutParams(

                TableRow.LayoutParams.MATCH_PARENT,

                TableRow.LayoutParams.WRAP_CONTENT));
        /** Creating a TextView to add to the row **/

        cosplaytxt = new TextView(this);

        cosplaytxt.setText(Cosplay_Name[i]);

        cosplaytxt.setTextColor(Color.RED);

        cosplaytxt.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        cosplaytxt.setLayoutParams(trs);

        cosplaytxt.setPadding(5, 5, 5, 5);
        tr.addView(cosplaytxt);
        tl.removeView(tr);
        tl.addView(tr, trs);

Here is the error

1 个答案:

答案 0 :(得分:0)

请将文本视图的高度和宽度设置为wrap_content。当前,文本视图的宽度为match_parent。试试这个。

tr.setLayoutParams(new TableRow.LayoutParams(
    TableRow.LayoutParams.WRAP_CONTENT,
    TableRow.LayoutParams.WRAP_CONTENT));