Android view layout_width - how to change programmatically?

后端 未结 4 763
刺人心
刺人心 2020-12-07 16:16

This is my view, and I wish to change layout_width to \"10dip\". How do I do so programmatically? Note, this is not a LinearLayout, it\'s a View.



        
相关标签:
4条回答
  • 2020-12-07 16:44

    You can set height and width like this also:

    viewinstance.setLayoutParams(new LayoutParams(width, height));
    
    0 讨论(0)
  • 2020-12-07 16:54

    Or simply:

    view.getLayoutParams().width = 400;
    view.requestLayout();
    
    0 讨论(0)
  • 2020-12-07 16:58

    try using

    View view_instance = (View)findViewById(R.id.nutrition_bar_filled);
    view_instance.setWidth(10);
    

    use Layoutparams to do so where you can set width and height like below.

    LayoutParams lp = new LayoutParams(10,LayoutParams.wrap_content);
    View_instance.setLayoutParams(lp);
    
    0 讨论(0)
  • 2020-12-07 17:09

    I believe your question is to change only width of view dynamically, whereas above methods will change layout properties completely to new one, so I suggest to getLayoutParams() from view first, then set width on layoutParams, and finally set layoutParams to the view, so following below steps to do the same.

    View view = findViewById(R.id.nutrition_bar_filled);
    LayoutParams layoutParams = view.getLayoutParams();
    layoutParams.width = newWidth;
    view.setLayoutParams(layoutParams);
    
    0 讨论(0)
提交回复
热议问题