re-setting a TextView height programmatically

后端 未结 6 1083
甜味超标
甜味超标 2020-12-24 11:20

I want to reset a textView height after I have added it to the main window in the xml file.

inside a RelativeLayout,

  

        
相关标签:
6条回答
  • 2020-12-24 11:59

    the sample way for this if you want to use dimen

    first, you should set size in dimen XML file.

    <dimen name="text_height">50dp</dimen>
    <dimen name="text_width">50dp</dimen>
    

    and

    text_l.getLayoutParams().height = 
         getResources().getDimensionPixelSize(R.dimen.text_height);
    
    text_l.getLayoutParams().width = 
         getResources().getDimensionPixelSize(R.dimen.text_width);
    

    Or

    if you want to set just int (for example we wanna set 50dp height and 100dp width)

    text_l.getLayoutParams().height = 50 * 3;
    text_l.getLayoutParams().width= 100 * 3;
    
    0 讨论(0)
  • 2020-12-24 11:59

    I know this is an old question, but for the sake of others who might find this, there are scenarios where you should call textView.requestLayout() after changing the layout parameters. While it my be fine to omit it if you are simply changing the layout parameters as a one time thing before the layout is drawn. In my case, I wanted to change the height parameter of a TextView based on a radio button selection using onCheckedChangedListener, but the TextView height would only update the first time it was drawn. Adding requestLayout() solved this problem.

    TextView tv;
    ViewGroup.LayoutParams params = tv.getLayoutParams();
    params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
    if(!tv.isInLayout()) {//make sure it isn't currently in a layout pass 
        tv.requestLayout();//request a layout pass
     }
    
    0 讨论(0)
  • 2020-12-24 12:07

    In Kotlin with DP to pixels translation

      changeTextHeight.setOnClickListener { view ->
    
             // random height for testing
            val randomHeightInDP = Random().nextFloat() * (50.0f - 10.0f) + 10
    
            // set Height in pixels
            hello.layoutParams.height = convertDpToPixel(randomHeightInDP, applicationContext)
             //refresh layout
            hello.requestLayout()
    
    
        }
    

    Convert DP to pixels, see this post:

    fun convertDpToPixel(dp: Float, context: Context): Int {
            return (dp * (context.resources.displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)).toInt()
        }
    
    0 讨论(0)
  • 2020-12-24 12:16

    Pragmatically you can set textview height like:

    private TextView mTxtView;
    int height = 50; //your textview height
    mTxtView.getLayoutParams().height = height;
    
    0 讨论(0)
  • 2020-12-24 12:16

    you can dynamically set width and height of textview by

    private TextView mTxtView;
    private int screenWidth, screenHeight;
    Display display = getWindowManager().getDefaultDisplay(); 
        screenWidth = display.getWidth();
        screenHeight = display.getHeight();
    
    
    LayoutParams params = mTxtView.getLayoutParams();
        params.width = screenWidth-30;
        mTxtView.setLayoutParams(params);
    
    0 讨论(0)
  • 2020-12-24 12:21

    You should change it via LayoutParams:

    LayoutParams params = (LayoutParams) textView.getLayoutParams();
    params.height = 70;
    textView.setLayoutParams(params);
    

    EDIT

    You should not use sizes in pixels in you code, use dimensions for this:

    dimens.xml:

    <dimen name="text_view_height">50dp</dimen>
    

    In code:

    params.height = getResources().getDimensionPixelSize(R.dimen.text_view_height);
    
    0 讨论(0)
提交回复
热议问题