How to dynamically set textview height android

前端 未结 3 1750
眼角桃花
眼角桃花 2020-12-08 05:25

In my application I need to set dynamic text to my textview so I want it to get resized dynamically. I have set:



        
相关标签:
3条回答
  • 2020-12-08 06:08

    As Konstantin said, this code will probably be ignored after you exceed 4 lines unless you remove android:maxLines="4"

    This is how you would set the height in code:

    TextView tv = (TextView) findViewById(R.id.TextView02);
    int height_in_pixels = tv.getLineCount() * tv.getLineHeight(); //approx height text
    tv.setHeight(height_in_pixels);
    

    If you want to use dip units, which allows your app to scale across multiple screen sizes, you would multiply the pixel count by the value returned by getResources().getDisplayMetrics().density;

    This depends on your desired behavior, but you might also consider having the TextView size fixed, and allowing the user to scroll the text:

    TextView tv = (TextView)findViewById(R.id.TextView02);
    tv.setMovementMethod(ScrollingMovementMethod.getInstance());
    
    0 讨论(0)
  • 2020-12-08 06:20
    TextView tv;
    ----------------------
    tv=new TextView(this);   // you take TextView id (R.id.textview1)
    
    LinearLayout.LayoutParams Params1 = new LinearLayout.LayoutParams(15,50);
    tv.setLayoutParams(Params1);   //you take linearlayout and relativelayout.
    
    0 讨论(0)
  • 2020-12-08 06:26

    Remove android:maxLines="4", it restricts height of your view to 4 lines of text.

    0 讨论(0)
提交回复
热议问题