Programatically changing Textview height and width

后端 未结 3 457
梦谈多话
梦谈多话 2021-01-29 00:02

I have a textview that I want to change the width to match_parent and height to remain at wrap_content. It is nested within a horizontal linearlayout. It is the 2nd in 3 textvie

相关标签:
3条回答
  • 2021-01-29 00:12

    What is your TextView parent layout? Linear, Relative or what? Sample if LinearLayout:

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    homeButton.setLayoutParams(params);
    

    You must create param base on its parent layout.

    0 讨论(0)
  • 2021-01-29 00:12

    You have already given the layout_width = "0dp" and layout_weight="1". So, when other two buttons will hide. This home button will take full width. But View.INVISIBLE won't remove them from taking width. You should use View.GONE so that even if they are not visible, they shouldn't take width.

    INVISIBLE:

     This view is invisible, but it still takes up space for layout purposes.
    

    GONE:

     This view is invisible, and it doesn't take any space for layout purposes.
    

    You don't need to set Layout Params again on Home TextView.

    0 讨论(0)
  • 2021-01-29 00:18

    Assuming your parent is a LinearLayout

    LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    homeButton.setLayoutParams(layoutParam);
    
    0 讨论(0)
提交回复
热议问题