首页 > 解决方案 > 如何修复 TextView 太大?

问题描述

我正在开发一个带有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);

这是错误

标签: javaandroid

解决方案


请将 textview 的高度和宽度设置为 wrap_content。目前 textview 的宽度是 match_parent。尝试这个。

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

推荐阅读