How to adjust text font size to fit textview

后端 未结 22 1734
無奈伤痛
無奈伤痛 2020-11-22 05:15

Is there any way in android to adjust the textsize in a textview to fit the space it occupies?

E.g. I\'m using a TableLayout and adding several Te

22条回答
  •  粉色の甜心
    2020-11-22 05:48

    /* get your context */
    Context c = getActivity().getApplicationContext();
    
    LinearLayout l = new LinearLayout(c);
    l.setOrientation(LinearLayout.VERTICAL);
    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 0);
    
    l.setLayoutParams(params);
    l.setBackgroundResource(R.drawable.border);
    
    TextView tv=new TextView(c);
    tv.setText(" your text here");
    
    /* set typeface if needed */
    Typeface tf = Typeface.createFromAsset(c.getAssets(),"fonts/VERDANA.TTF");  
    tv.setTypeface(tf);
    
    // LayoutParams lp = new LayoutParams();
    
    tv.setTextColor(Color.parseColor("#282828"));
    
    tv.setGravity(Gravity.CENTER | Gravity.BOTTOM);
    //  tv.setLayoutParams(lp);
    
    tv.setTextSize(20);
    l.addView(tv);
    
    return l;
    

提交回复
热议问题